2016-12-28 37 views
1

解釋我想要做什麼有點困難(對於難以閱讀的標題抱歉),但基本上我想總結重複行如下:如何通過多列條件求和(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 

我怎樣才能做到這一點?

+0

是什麼類型'sales'列? – potashin

+0

在你想要的輸出中,你確定EntryID對每一行都是正確的嗎? (我會想象一些行的EntryID不止一個...) – Grantly

回答

4

的SQL語句的樣子:

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

格式化查詢:) – GurV

0

細節欠缺,但它聽起來像一個簡單的GROUP BY對我說:

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