如何通过多列条件求和(SQL)

时间:2016-12-28 18:17:21

标签: sql

解释我正在尝试做什么有点困难(对于难以阅读的标题感到抱歉),但基本上我想要重复这样的行:

表A:

EntryID| Country | Region | Sales 
101    | USA     | North  | $10
102    | USA     | North  | $5
103    | USA     | South  | $15
104    | USA     | South  | $10
105    | CHINA   | North  | $5
106    | CHINA   | North  | $5
107    | CHINA   | South  | $10
108    | CHINA   | South  | $20

期望输出

EntryID| Country | Region | Sales 
101    | USA     | North  | $15
103    | USA     | South  | $25
105    | CHINA   | North  | $10
107    | CHINA   | South  | $30

我该怎么做?

2 个答案:

答案 0 :(得分:4)

SQL语句必须如下所示:

SELECT MIN(EntryID) AS EntryID,
  Country,
  Region,
  SUM(Sales) AS Sales
FROM TABLE
GROUP BY Country,
  Region

答案 1 :(得分:0)

缺乏详细信息,但对我来说这听起来很简单GROUP BY

select min(EntryID), Country, Region, sum(sales)
  from tbl
 group by Country, Region