从数据库中选择行和sum / group by

时间:2017-08-17 11:52:32

标签: sql sql-server

我目前正在努力解决这个SQL语句的逻辑问题。

我有一个包含以下列的列表:

Country, Customer, Year, Product Purchased( 5 different products) and Revenue.

我正在寻找一份选择声明,该声明通过国家/地区,客户名称,每个购买的产品,每年为客户提供的每种产品的收入。

SELECT Country, Customer, Year, Product, Revenue
FROM table
GROUP BY Country, Customer, Year, Product Purchased, Revenue

国家/地区客户年度产品收入总收入 英国客户A 2017产品A 100 200 英国客户A 2017产品B 100 200 英国客户A 2016产品A 100 100 有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

SELECT sum(Revenue), Country, Customer, Year, Product
FROM table
GROUP BY Country, Customer, Year, Product 

Live demo to illustrate the result

答案 1 :(得分:0)

SELECT SUM(Revenue),Country, Customer, YEAR, Product 
FROM TABLE
GROUP BY Country, Customer, YEAR, Product Purchased