tags:

views:

145

answers:

4

hi i hve an sqlite db which contain transactions each transaction has an price and has an transDate

i want to retrieve the sum of the transaction group by month

so the retrieved records should be like the following

Price    month
230        2
500        3
400        4

pleas any help

+1  A: 

You can group on the start of the month:

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

Try the following:

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

Use the corresponding page in SQLite documentation for future references.

newtover
+1  A: 

it is always good while you group by MONTH it should check YEAR also

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

FOR SQLITE

select SUM(transaction) as Price, 
       strftime("%m-%Y", transDate) as 'month-year' 
       from transaction group by strftime("%m-%Y", transDate);
Salil
+1 for the year part, I did not specify it in my solution.
newtover
"no such function: date_format"
Andomar
+1  A: 
SELECT
    SUM(Price) as Price, strftime('%m', myDateCol) as Month
FROM
    myTable
GROUP BY
    strftime('%m', myDateCol)
Veer
Works, but potentially sums the value of March 2009 with March 2010 for month 3
Andomar