Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return number of tickets created per week with SQL query

Tags:

sql

postgresql

I have a table of tickets, common_ticket, with a column called creation_date, which holds the date of creation.

I want to count how many tickets were created each week for the past few months. I am having trouble writing a SQL query to return such information. How it is returned is not really important as long as there is a distinct number for each separate week.

Does anyone have any ideas on how to do this?

like image 512
Yottagray Avatar asked Mar 08 '11 18:03

Yottagray


People also ask

How do I get weekly week data in SQL?

WEEK() function in MySQL is used to find week number for a given date. If the date is NULL, the WEEK() function will return NULL. Otherwise, it returns the value of week which ranges between 0 to 53. The date or datetime from which we want to extract the week.

How do I get weekly count in SQL?

How Do You Group Data by Week in SQL Server? SQL Server provides a function called DATEPART() , which returns a specified part (year, quarter, month, week, hour, minute, etc.) of a specified date. ORDER BY DATEPART(week, RegistrationDate);

How do I get a week return from a date in SQL?

To get yesterday's date, you need to subtract one day from today's date. Use GETDATE() to get today's date (the type is datetime ) and cast it to date . In SQL Server, you can subtract or add any number of days using the DATEADD() function. The DATEADD() function takes three arguments: datepart , number , and date .

Is there a week function in SQL?

MySQL WEEK() Function The WEEK() function returns the week number for a given date (a number from 0 to 53).


1 Answers

Something like:

SELECT extract(week from creation_date), 
       extract(year from creation_date),
       count(*)
FROM tickets
GROUP BY extract(week from creation_date), 
         extract(year from creation_date)
like image 151
a_horse_with_no_name Avatar answered Sep 24 '22 18:09

a_horse_with_no_name