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?
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 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);
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 .
MySQL WEEK() Function The WEEK() function returns the week number for a given date (a number from 0 to 53).
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)
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