2010-05-24 94 views
9

我有一個SQLite數據庫包含交易,他們每個人都有價格transDateSQLite按月份分組

我想檢索按月份分組的交易總和。檢索到的記錄應該是這樣的:

Price month 
230  2 
500  3 
400  4 
+0

哪些表中的列? – Mark 2010-05-24 09:16:12

回答

20

這是一件好事,而你組每月應檢查今年還

select SUM(transaction) as Price, 
     DATE_FORMAT(transDate, "%m-%Y") as 'month-year' 
     from transaction group by DATE_FORMAT(transDate, "%m-%Y"); 

SQLite的

select SUM(transaction) as Price, 
     strftime("%m-%Y", transDate) as 'month-year' 
     from transaction group by strftime("%m-%Y", transDate); 
+0

年份的+1,我沒有在我的解決方案中指定它。 – newtover 2010-05-24 09:20:34

+0

「no such function:date_format」 – Andomar 2010-05-24 09:25:09

+0

如果你使用的是android,這可能會有幫助strftime('%m-%Y',transDate,'unixepoch') – dwbrito 2016-05-23 17:24:11

1

您可以在本月啓動組:

select date(DateColumn, 'start of month') 
,  sum(TransactionValueColumn) 
from YourTable 
group by 
     date(DateColumn, 'start of month') 
1

嘗試以下操作:

SELECT SUM(price), strftime('%m', transDate) as month 
FROM your_table 
GROUP BY strftime('%m', transDate); 

使用corresponding page SQLite的文檔中爲將來的參考。

2
SELECT 
    SUM(Price) as Price, strftime('%m', myDateCol) as Month 
FROM 
    myTable 
GROUP BY 
    strftime('%m', myDateCol) 
+0

工程,但可能將2009年3月與2010年3月3個月 – Andomar 2010-05-24 09:27:23