1. The following table, for the 2017 Q1 quarter from A_country to other cities of the highest volume of the company, and output volume sum
ID |
from_country |
to_country |
company_name |
company_count |
year |
quarter |
month |
|
|
|
|
|
|
|
|
Select * FROM (select sum (company_count) as Top_count,company_namefrom testdatawhere ' year ' = ' and ' Quarter ' = ' Q1 ' and from_country= ' A_country ' GROUP by From_country,to_country,company_name, ' year ', ' Quarter ' ORDER by Company_count DESC) as Agroup by From_country,to_country
2. Conditions same as 1, while querying Company B
SELECT * FROM (select * FROM (select sum (company_count) as Top_count,company_name,company_countfrom Testdatawhere ' Year ' = ' and ' quarter ' = ' Q1 ' GROUP by From_country,to_country,company_name, ' year ', ' Quarter ' ORDER by Company_count DESC) as Agroup by From_country,to_country) as Topcompanyinner JOIN (SELECT b_count,to_country from ( SELECT sum (company_count) as b_count,company_name as B,to_countryfrom testdatawhere ' year ' = ' and ' quarter ' = ' Q1 '
SQL is grouped by multiple fields and queries each group for TOP1