I have an SQLite database which contains transactions, each of them having a price and a transDate.
I want to retrieve the sum of the transactions grouped by month. The retrieved records should be like the following:
Price month 230 2 500 3 400 4
select strftime('%m', dateField) as Month ... Save this answer.
The GROUP BY clause a selected group of rows into summary rows by values of one or more columns. The GROUP BY clause returns one row for each group. For each group, you can apply an aggregate function such as MIN , MAX , SUM , COUNT , or AVG to provide more information about each group.
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);
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With