SQL is grouped by multiple fields and queries each group for TOP1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.