2016-08-03 44 views
0

我按照表:部分和

2016-01  111.00  100.00 
2016-01  111.00  11.00 
2016-02  222.00  100.00 
2016-02  222.00  100.00 
2016-02  222.00  22.00 
2016-01  333.00  200.00 
2016-01  333.00  133.00 

我需要:

2016-01  444.00 
2016-02  222.00 

我怎麼能接受這個數據?按期間求和,但只有明顯的價值?

+0

看起來要加入兩個表得到這個數據集?如果是這樣的話,你可以在加入數據之前對第二列「GROUP」進行「SUM」處理,以獲得第三列中的數據。 – iamdave

回答

2

使用SUM()DISTINCT

SELECT DateColName ,SUM(DISTINCT ColName) 
FROM TABLENAME 
GROUP BY DateColName 
0
;with cte 
as 
(select 
date,col1,col2 
,row_number() over (partition by date,col1 order by date) as rownum 
) 
select 
date,sum(col1) 
from cte 
where rownum=1 
group by date 
1

SUM() + DISTINCT + GROUP BY是你的朋友。

重新創建測試用例:

sqlite> CREATE TABLE money(month TEXT, q1 FLOAT, q2 FLOAT); 
sqlite> INSERT INTO money VALUES('2016-01','111.00','100.00'); 
sqlite> INSERT INTO money VALUES('2016-01','111.00','11.00'); 
sqlite> INSERT INTO money VALUES('2016-02','222.00','100.00'); 
sqlite> INSERT INTO money VALUES('2016-02','222.00','100.00'); 
sqlite> INSERT INTO money VALUES('2016-02','222.00','22.00'); 
sqlite> INSERT INTO money VALUES('2016-01','333.00','200.00'); 
sqlite> INSERT INTO money VALUES('2016-01','333.00','133.00'); 

檢查數據是一樣的你:

sqlite> SELECT * FROM money; 
2016-01|111.0|100.0 
2016-01|111.0|11.0 
2016-02|222.0|100.0 
2016-02|222.0|100.0 
2016-02|222.0|22.0 
2016-01|333.0|200.0 
2016-01|333.0|133.0 

運行查詢:

sqlite> SELECT month, SUM(DISTINCT q1) FROM money GROUP BY month; 
2016-01|444.0 
2016-02|222.0 
+0

DISTINCT :)謝謝:) – kole1108