Oracle SQL Query to order by column value in different table - sql

I'm rather new to sql and am struggling with a statement. For simplicity, let's say I have two tables -
Table - JourneyOrder, and Table - ItemsToBuy.
JourneyOrder has list of cities, index of a particular city in a journey and who is taking the journey.
So:
City Index Name
London 1 John
Sydney 2 John
Paris 3 John
Berlin 1 Jack
Paris 2 Jack
The other table has a list of items to buy, containing location (as in City), who needs to buy an item, and different characteristics of an item.
Item Location Weight Price Responsible
Pencil London 1 2 John
Glue London 2 5 John
Car Paris 1000 12000 John
Wallet Paris 3 20 Jack
I want to select all items a particular person needs to buy and order them by location. So if John's first route is London, I want items you can buy in London displayed first.
At the moment, I can use the following:
SELECT itb.item, itb.location, itb.price
FROM ItemsToBuy itb,
WHERE itb.responsible = 'John'
ORDER BY CASE
WHEN itb.location = 'London' THEN '1'
WHEN itb.location = 'Sydney' THEN '2'
WHEN itb.location = 'Paris' THEN '3'
Now this is obviously terrible, as I need to 'hardcode' the order by part according to the journey each responsible is taking, how can I make it so it matches location of items table to city from journey table and uses the index from the journey table when these match.

I think you just want a join:
SELECT itb.item, itb.location, itb.price
FROM ItemsToBuy itb JOIN
JourneyOrder jo
ON itb.name = jo.name AND itb.location = jo.city
WHERE itb.responsible = 'John'
ORDER BY jo.index;

Related

Need to add new column in SQL View and get the value from another row based on condition

There is a table where one of the column stores their local language values and I would like to add another column in SQL view which will have values in English only which we can get it from other country.
here's the sample data
id
country
name
1
Belgium
Product
1
Italy
Prodotto
1
Spain
Product
2
Belgium
Used Car
2
Italy
Usato
2
Spain
Used Car
Expected Results :
id
country
name
English Name
1
Belgium
Product
Product
1
Italy
Prodotto
Product
1
Spain
Product
Product
2
Belgium
Used Car
Used Car
2
Italy
Usato
Used Car
2
Spain
Used Car
Used Car
Please help me with adding new column "English Name" in SQL View which can be derived from other countries. So here, Only Italy will have value other than English so in case Country Italy , I need to get the value from either Beligum/Spain.
Thanks in advance!
You can use the FIRST_VALUE window function to select the name by ordering on when your country is Italy (NULL values are ordered first).
SELECT *, FIRST_VALUE(name) OVER(
PARTITION BY id
ORDER BY CASE WHEN country = 'Italy' THEN 0 END
) AS EnglishName
FROM tab
Check the demo here.

How group by count from multiple tables

I have 3 different tables, country, city, and customer. Tables are shown below:
country table:
id country_name
1 UK
2 US
3 Brazil
:
n Canada
city table
id city_name postal_code country_id
1 London 30090 1
2 Dallas 20909 2
3 Rio 29090 3
4 Atlanta 30318 2
:
n Vancouver 32230 n
customer table
id customer_name city_id
1 John 1
2 Pete 3
3 Dave 2
4 May 2
5 Chuck 4
6 Sam 3
7 Henry 3
***country.id is references city.country_id, and city.id is references customer.city_id
I want to write a query that can extract the country name, city name and the count of the customer of the associate city. But with one condition, the query will return all cities with more customers than the average number of customers of all cities
It will look something like below, this is the correct output
UK London 2
Brazil Rio 3
but I kept getting this output, which isn't correct
UK London 2
US Dallas 2
US Atlanta 1
Brazil Rio 3
I fixed my SQL query but it doesn't give me the result that I want
SELECT country.country_name, city.city_name, COUNT(customer.city_id) from country
JOIN city on country.id = city.country_id
JOIN customer on city.id = customer.city_id
Group by city_name,country.country_name;
I am wondering how can I do this and fix my code?
add country.country_name in group by
SELECT country.country_name, city.city_name, COUNT(customer.city_id) from country
JOIN city on country.id = city.country_id
JOIN customer on city.id = customer.city_id
Group by city_name,country.country_name
You are missing country.country_name in the query it will give you error. As a general rule all columns on which aggregate function is not applied should be part of group by clause.
So either you write your query without country_name in the select or add country_name in the group by clause.

Is this multiple join on 2 tables possible?

I have 2 tables and I am having trouble joining it to give me the desired output.
First table is called Future. It is future meetings I have.
Date Name Subject Importance Location
7/08/2020 David Work 1 London
7/08/2020 George Updates 2 New York
7/08/2020 Frank New Appointments 5 London
7/08/2020 Steph Policy 1 Paris
The second table is called Previous. It is previous meetings I have had.
Date Name Subject Importance Location Time Rating
1/08/2020 David Work 3 London 23.50 4
2/10/2018 David Emails 3 New York 18.20 3
1/08/2019 George New Appointments5 London 55.10 2
3/04/2020 Steph Dismissal 1 Paris 33.20 5
Now what I need to is to reference my previous table by name to see the previous meetings I have had with this person and I want all the data from the Previous Table there. I also need to limit it to only showing maximum 5 previous meetings with each person.
Date Name Subject Importance Location Time Rating
7/08/2020 David Work 1 London - -
1/08/2020 David Work 3 London 23.50 4
2/10/2018 David Emails 3 New York 18.20 3
7/08/2020 George Updates 2 New York - -
1/08/2019 George New Appointments5 London 55.10 2
The Name column will need to be a left join, but then i need to just do a regular join on the other columns. Also unsure how to limit the name results to a maximum of 5 of the same value. Thanks for your help in advance.
Basically, you want union all:
select m.*
from ((select Date, Name, Subject, Importance, Location, NULL as time, NULL as rating
from future
) union all
(select Date, Name, Subject, Importance, Location, time, rating
from previous
)
) m
group by name, date desc;
You can apply other conditions to this result. It is not clear what other conditions you really want, but this is a start.

Many to one merging sql

I have three tables as below:
First Table Second Table Third Table
Name PIN Id City City_id
David 1948 1 Roma 3
Susan 1245 2 Berlin 2
Jack 1578 3 New York 3
Hans 1247 2
Rose 8745 1
I want to merge first and second table according to third table. Result will be: Person
Name PIN City
David 1948 New York
Susan 1245 Berlin
Jack 1578 New York
Hans 1247 Berlin
Rose 8745 Roma
Firsty I can merge second and third table and then merge the result table with first table but I want to solve this problem without a medium table. How can I handle this? How can I combine first table's rows in sequence with a specified row in second table according to third table?
You would need a fourth table, PersonCity, with PersonID and CityID to link together. Think of relational databases like a grid (spreadsheet, roads). If you're going North and the street you want to get on is parallel (think |^| |^|) you're gonna need to use a different road that links the two. Currently, you have no such path.
The short answer is that your tables are not adequate for the task, what you need is along the lines of:
Table_1 Table_2 Table_3
Id Name PIN Id City Name_id City_id
1 David 1948 1 Roma 1 3
2 Susan 1245 2 Berlin 2 2
3 Jack 1578 3 New York 3 3
4 Hans 1247 4 2
5 Rose 8745 5 1
Then you can do your query as follow:
SELECT T1.Name, T1.PIN, T2.City
FROM Table_1 T1 LEFT JOIN Table_3 T2 ON T1.Id = T3.Name_id
LEFT JOIN Table_2 ON T3.City_id = T2.Id
ORDER BY T1.Name
Or you could ORDER BY City, name
I have good news and bad news.
The good news, Given the tables the way they were originally specified, in Oracle, this will give you something that looks like what you are asking:
---
--- Pay attention, This looks right but it is not!
---
select name,pin,city from
( select name,pin,rownum rn from first ) a,
( select city,id from second) b,
( select id,rownum rn from third ) c
where
a.rn=c.rn AND
b.id=c.id;
NAME PIN CITY
-------------------- ---- --------------------
Rose 8745 Roma
Susan 1245 Berlin
Hans 1247 Berlin
David 1948 New York
Jack 1578 New York
The bad news is this does not really work and is cheating. You will get results but they may not be what you would expect and they won't necessarily be consistent.
The database orders records in its own order. If you don't specify an order by clause, you get what they give you, which may not be what you want. This is cheating because Oracle does not REALLY support using rownum in this way because you can't bet on what you will get. This won't work in most other databases.
The only correct way is what #daShier gave, where you have to add something, say, ID, to allow connecting the rows in the order you want.

Find last match with latest inserted name in table

I have a table PersonalInfo having columns such as Id, Name, Address.
Suppose I have following data
1 John US
2 Mark UK
3 John UK
4 David US
Now when I insert following new record
5 John China
I want to update the last record having same name as the new one for example as shown here the record 3 John UK will be updated to 3 John China.
When I insert the 5th record the table should be
1 John US
2 Mark UK
3 John China
4 David US
5 John China
what sql query should I use?
Assuming the Id is auto-incremented, it would be the highest value for that Name and Address combination.
UPDATE PersonalInfo
SET Address = 'US'
WHERE ID = (SELECT MAX(Id)
FROM PersonalInfo
WHERE Name = 'John'
AND Address = 'UK'
)