简体   繁体   中英

SQL query to sum total turnover for countries

Can someone explain how can i get the sum turnover for each country based on this query?

SELECT ZipCodes.Country AS country, (LineItem.price*LineItem.quantity) as turnover
FROM LineItem
INNER JOIN [Order] ON [Order].id = LineItem.order_id
INNER JOIN Party ON Party.id = [Order].party_id
INNER JOIN ZipCodes ON ZipCodes.id = Party.party_zip_code_id
GROUP BY ZipCodes.Country

I am getting:

Msg 8120, Level 16, State 1, Line 1 Column 'LineItem.price' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Msg 8120, Level 16, State 1, Line 1 Column 'LineItem.quantity' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Simply add the sum to your query:

SELECT 
    ZipCodes.Country AS country, 
    sum(LineItem.price*LineItem.quantity) as turnover -- Add here the sum
FROM 
    LineItem
    INNER JOIN [Order] ON [Order].id = LineItem.order_id
    INNER JOIN Party ON Party.id = [Order].party_id
    INNER JOIN ZipCodes ON ZipCodes.id = Party.party_zip_code_id
GROUP BY 
    ZipCodes.Country

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM