我有查詢結果如下:總和加入重複行(SQL)時
SELECT ... ON CIA_factbook_dataset.my_name = World_Bank_dataset.my_name ...
+----------------+------+-------------+-----------------+---------+--------+
| my_name | Year | CIA_name | World_Bank_name | CIA_GDP | WB_GDP |
+----------------+------+-------------+-----------------+---------+--------+
| United Kingdom | 2010 | UK | United Kingdom | 2850 | 2800 |
| United Kingdom | 2010 | UK | Channel Islands | 2850 | 11 |
| Cyprus | 2010 | CYPRUS TURK | CYPRUS TURK | 22 | 22 |
| Cyprus | 2010 | CYPRUS TURK | CYPRUS GRK | 22 | 33 |
| Cyprus | 2010 | CYPRUS GRK | CYPRUS TURK | 33 | 22 |
| Cyprus | 2010 | CYPRUS GRK | CYPRUS GRK | 33 | 33 |
+----------------+------+-------------+-----------------+---------+--------+
我需要計算的分國別數據的總和,但如果我只用GROUP BY my_name,year
,它計算幾次相同數目的總和。
最終的結果應該是:
+----------------+------+---------+--------+
| my_name | Year | CIA_GDP | WB_GDP |
+----------------+------+---------+--------+
| United Kingdom | 2010 | 2850 | 2811 |
| Cyprus | 2010 | 55 | 55 |
+----------------+------+---------+--------+
相反的:
+----------------+------+---------+--------+
| my_name | Year | CIA_GDP | WB_GDP |
+----------------+------+---------+--------+
| United Kingdom | 2010 | 5700 | 2811 |
| Cyprus | 2010 | 110 | 110 |
+----------------+------+---------+--------+
如何實現這一目標?
比使用SUM(distinct CIA_GDP),SUM(distinct WB_GDP)
更好的方法嗎?
(理論上,土耳其塞浦路斯和希臘塞浦路斯的GDP可能是相同的)
等待,爲什麼是'英國(CIA_GDP)'的'MAX()'和'塞浦路斯(CIA_GDP)'的'SUM( )'在你想要的輸出? – Blag
誰在談論MAX?! –
好吧,我知道了,我的壞,2min – Blag