SQL查询以总计各国的总营业额

时间:2013-05-27 22:01:23

标签: sql sql-server sql-server-2008

有人可以解释如何根据此查询获得每个国家/地区的总营业额?

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

我得到了:

  

消息8120,级别16,状态1,行1列“LineItem.price”在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

     

消息8120,级别16,状态1,行1列“LineItem.quantity”在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

1 个答案:

答案 0 :(得分:3)

只需将sum添加到您的查询中:

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