Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last 12 months, group by week

Tags:

I have a table with a column REGDATE, a registration date (YYYY-MM-DD HH:MM:SS). I would like to show an histogram (ExtJS) in order to understand in which period of the years users are signing up. I would like to do this for the past twelve months with respect to the current date and to group dates by week.

Any hints?

like image 685
Danilo Avatar asked Sep 24 '10 15:09

Danilo


People also ask

How can I get last 12 months data in SQL?

How to Get Last 12 Months Sales Data in SQL. mysql> select * from sales where order_date> now() - INTERVAL 12 month; In the above query, we use system function now() to get current datetime. Then we use INTERVAL clause to filter those records where order_date falls after an interval of 12 months before present datetime ...

How do I Group A timestamp by month?

You can group month and year with the help of function DATE_FORMAT() in MySQL. The GROUP BY clause is also used.

How do I get last 7 days record in SQL?

Here's the SQL query to get records from last 7 days in MySQL. In the above query we select those records where order_date falls after a past interval of 7 days. We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.


Video Answer


1 Answers

FWIW in PostgreSQL, Karaszi has an answer that works, but there is a faster query:

SELECT date_trunc('week', REGDATE) AS "Week" , count(*) AS "No. of users" FROM <<TABLE>> WHERE REGDATE > now() - interval '12 months'  GROUP BY 1 ORDER BY 1; 

I based this off the work of Ben Goodacre

like image 184
Tom Gerken Avatar answered Nov 13 '22 09:11

Tom Gerken