2015-08-14 69 views
-1

總的我有這個表有兩列:SUM每月

Price  Date 
45.00  12/06/2015 12:32:54 AM 
455.98  22/06/2015 11:00:32 AM 
32.00  08/07/2015 09:11:45 AM 
98.00  11/07/2015 19:22:32 PM 

(日期格式爲DD/MM/YYYY)

我需要得到價格的總和按月分組,爲此,我需要在sqlite中找到一個代碼,以便僅在該月份(06 ...)(... 07 ...)中刪除該日期的某些部分。以下是我的代碼用於選擇某些部分:

SELECT SUM(CAST(price as INTEGER)) as TOTALPERMONTH FROM cart_history GROUP BY ....... ORDER BY ID 

我怎麼可以按該行的某些部分分組?

+0

爲什麼使用該日期格式?你不能改變它嗎? –

回答

-1

使用STRFTIME功能

select SUM(CAST(price as INTEGER)) as Total, 
     strftime('%m', dateField) as Month 
From yourtable 
Group by strftime('%m', dateField) 
+0

我的列日期是varchar,而不是datetime,有問題嗎? – LettersBa

-1

使用HAVING條款。

SELECT SUM(CAST(price as INTEGER)) as TOTALPERMONTH 
FROM cart_history,Date 
GROUP BY Date 
HAVING Date IN('your dates ')'