I have the below 2 tables
Table Name: Port
City Code
------------------
City A 001
City B 002
City C 003
City D 004
City E 005
City F 006
City G 007
Table Name: Shipments
Code Shipments
------------------
001 5
001 4
002 2
003 4
003 3
003 4
004 1
005 1
006 1
007 2
I want to get the list of Cities where maximum shipments take place.
Answer:
City Total Shipments
------------------------
City C 11
City D 1
City E 1
City F 1
Can someone help me with the SQL query for this.
select c.city as City, sum(s.shipments) as Shipments
from
port c
inner join
shipments s on s.code = c.code
group by c.code
order by sum(s.shipments) asc;
Check this out and let me know.
You can use a Common Table Expression (CTE) to first aggregate the shipments for each City. The min and max records can then be returned as follows:
WITH cte AS ( SELECT P.city AS City,
SUM(S.shipments) AS TotalShipments
FROM Port P
INNER JOIN Shipments S ON S.code = P.code
GROUP BY P.city )
SELECT *
FROM cte c
WHERE c.TotalShipments IN ( SELECT MAX(c2.TotalShipments) RequiredTotalShipments
FROM cte c2
UNION
SELECT MIN(c3.TotalShipments)
FROM cte c3 )
Related
I want to take the following table...
City
Company
Revenue
New York
A
400
New York
B
300
Singapore
B
400
Washington D.C.
C
400
London
D
400
and produce the below version.
City
Company
Present
Revenue
New York
A
1
400
New York
B
1
300
New York
C
0
0
New York
D
0
0
Singapore
A
0
0
Singapore
B
1
400
Singapore
C
0
0
Singapore
D
0
0
...
...
London
D
1
400
So, basically filling extra company values to encompass the entire dataset, adding a 1 in the "Present" field if they are present, otherwise zero...
I hope my question makes sense!
You can pull this off by first making a result set that has every combination of company and city. A cross join will do the trick here:
SELECT companies.company, cities.city
FROM (SELECT DISTINCT Company FROM yourtable) companies,
(SELECT DISTINCT City FROM yourtable) cities
We can now LEFT JOIN from that result set into your table to get to the final result set:
WITH all_values AS
(
SELECT companies.company, cities.city
FROM (SELECT DISTINCT Company FROM yourtable) companies,
(SELECT DISTINCT City FROM yourtable) cities
)
SELECT all_values.company,
all_values.city,
CASE WHEN yourtable.company IS NOT NULL THEN 1 ELSE 0 END as Present,
yourtable.Revenue
FROM
all_values
LEFT OUTER JOIN yourtable
ON all_values.company = yourtable.company
AND all_values.city = yourtable.city;
select city.city
, company.company
, coalesce(t.Revenue, 0)
, case when t.revenue is not null then 1 else 0 end as Present
from (select distinct company from tablename) company
cross join (select distinct City from tablename) city
left join tablename t
on t.city = city.city
and t.company = company.company
I have two tables
Cities:
id name
------------
1 Helsinki
2 Tukholma
3 Oslo
4 Turku
Flights
id where_id to_id
---------------------
1 1 2
2 1 3
3 2 3
4 2 4
I want to get this result
Helsinki Tukholma
Helsinki Oslo
Tukholma Oslo
Tukholma Turku
How do I compose the query? Result has two name columns and I can't get around it?
You can join twice:
select c1.name where_city, c2.name to_city
from flights f
inner join cities c1 on c1.id = f.where_id
inner join cities c2 on c2.id = f.to_id
You need two joins:
select f.*, cw.name, ct.name
from flights f join
cities cw
on f.where_id = cw.id join
cities ct
on f.to_id = ct.id;
I found this solution. Pretty clear. No JOINs
SELECT A.name, B.name FROM cities A, cities B, flights F WHERE F.where_id=A.id AND L.to_id=B.id;
I have the following table:
Code ParentCode oItem
-----------------------------
A null Item 001
B A Item 002
C A Item 003
D C Item 004
E B Item 005
Now, I want to have the row number then the result should be:
Rn Code ParentCode oItem
------------------------------------------
1 A null Item 001
2 B A Item 002
3 C A Item 003
4 D C Item 004
5 E B Item 005
My question is, How's the query to get the following result:
Rn Code RnParent ParentCode oItem
--------------------------------------------------------
1 A null null Item 001
2 B 1 A Item 002
3 C 1 A Item 003
4 D 3 C Item 004
5 E 2 B Item 005
If you see on the result table, parentcode is actually the code and I need to know the id of the parentcode base on the code of Rn.
Please advise.
Thank you.
If I understand correctly, you just want the rn for the parentcode. You can get this with a join:
with t as (
select row_number() over (order by code) as rn, t.*
from t
)
select t.*, tp.rn as parentrn
from t left join
t tp
on t.parentcode = tp.code
I have these tables
Customers
CustomerNumber date
001 8/1/2017
002 8/2/2017
003 8/3/2017
Tags
Index Tag Description
1 NEW New customer
2 OTHER Some other tag
Customers_Tags
TagIndex CustomerNumber
1 001
1 002
2 002
2 003
How can I, in a single query, get all customers with tag 1 and also any other tags those customers have? So if looking for tag 1 get:
customer tag date
001 1 8/1/2017
002 1 8/2/2017
002 2 8/2/2017
using exists() to get all customers and tags when that customer has a tag of index 1:
select ct.customernumber, ct.tagindex, c.date
from customers c
inner join customers_tags ct
on c.customernumber = ct.customernumber
where exists (
select 1
from customers_tags i
where i.customernumber = ct.customernumber
and i.tagindex = 1
)
or using in():
select ct.customernumber, ct.tagindex, c.date
from customers c
inner join customers_tags ct
on c.customernumber = ct.customernumber
where c.customernumber in (
select i.customernumber
from customers_tags i
where i.tagindex = 1
)
I have a query that returns records as below
vw_EmployeeReferenceNumbers
NAME Number
---- ------
AA 123
AA 234
AA 456
I have another table that returns records like so
AllEmployees
AllNames
----------
AA
BB
CC
I want to output a recordset like so
NAME Number
---- ------
AA 123
AA 234
AA 456
BB 123
BB 234
BB 456
CC 123
CC 234
CC 456
I dont want to use Cursors at all. I cant modify the view vw_EmployeeReferenceNumbers or the table AllEmployees. Can this be done in SQL?
What I have so far that doesn't work is:
select name, number
from
(select Name, number, 1 as id from vw_EmployeeReferenceNumbers
) as A
left join
(select name, 1 as id from AllEmployees
) as B
on A.id = B.id
Use cross join:
select e.name, ern.number
from AllEmployees e cross join
vw_EmployeeReferenceNumbers ern;
SELECT e.name,
ern.number
FROM AllEmployees e INNER JOIN
EmployeeReferenceNumbers ern ON 1=1
ORDER BY e.name