Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by month in SQLite

Tags:

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 
like image 725
Hiyasat Avatar asked May 24 '10 09:05

Hiyasat


People also ask

How to get the month from a date in SQLite?

select strftime('%m', dateField) as Month ... Save this answer.

What does group by do in SQLite?

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.


2 Answers

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); 
like image 182
Salil Avatar answered Sep 19 '22 07:09

Salil


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') 
like image 33
Andomar Avatar answered Sep 20 '22 07:09

Andomar