MySQL grouping by week, based on a date column? - mysql

I have a table with a date column and I would like to try and group by, using a week as a time reference in order to count how many rows occured per week. I have done this for days, using GROUP BY Date(Date_Column) but i'm unsure how to do this by week?
Thanks

SELECT ...
FROM ....
GROUP BY YEAR(Date_column), WEEKOFYEAR(Date_Column);

Try to put a GROUP BY YEARWEEK(date_column) at the end of your query - this will take in consideration also the year the date is in.

SELECT week(Date_Column)
FROM ....
GROUP BY week(Date_Column);

SELECT WEEKOFYEAR("2017-01-01"),YEARWEEK("2017-01-01"),WEEK("2017-01-01");
Outputs:
WEEKOFYEAR("2017-01-01") YEARWEEK("2017-01-01") WEEK("2017-01-01")
52 201701 1
Looks like YEARWEEK is the best solution. No need to concat the year.

SELECT CONCAT(YEAR(Date_Column),'/',WEEK(Date_Column)) AS efdt
FROM ....
GROUP BY efdt;

Related

Calculate AVG base on dayweek SQL

I'm working whit a MariaDB database.
I need to know, for every day in a certain time the avg of a count.
I'd tried somethink like this.
SELECT AVG(dayShipments), weekdays
FROM (SELECT COUNT(idShipment) as "dayShipments", WEEKDAY(dateShipments) as "weekdays"
FROM weekdays
WHERE dateShipments BETWEEN '2021-05-01'AND '2021-05-21'
GROUP BY dateShipments) as t1
GROUP BY weekdays
My boss told me that this query ignore the day where I don't have any Shipment.
How can i inlude that?
Sorry for my bad English and thanks for helping me
If you want to summary by day-of-the-week (which is what your query appears to be doing. And you want to treat days with no shipments as 0, then use SUM() and division:
SELECT WEEKDAY(dateShipments) as weekday,
COUNT(*) / 3 as dayShipments as avg_per_day
FROM weekdays
WHERE dateShipments BETWEEN '2021-05-01'AND '2021-05-21'
GROUP BY weekday;
The 3 is because the query spans three weeks.
First of all don't worry about your english. It's good enough.
Secondly, your boss is right. If you have no records in "weekdays" table for specific day, the mentioned day will never show up with this query.
For solving problem I think you need to have a temporary table for day of week and left join with your t1 table.

Count the difference of time in a table using postgre sql query

I have a table named Job with created_date and start_date columns.
I need to count the difference in start_date and created_date and find the count
count difference of 1 day 2 days 3 days and so on
Help me with it, I am new in sql queries
You can try the following in SQL
SELECT TRUNC(START_DATE) - TRUNC(CREATED_DATE) DAYS FROM JOB;
I found the Answer in postgresql it is:
select date_part('day',start_time - created_on) as Difference , count(*) from Job
thanks for the replies to all

How would you query a cumulative sum over x amount of days?

I'm trying to find the cumulative sum of sessions of a link for its first 3 days. I tried this but it doesn't seem to take the date clause into account:
select
date,
link,
sum(sessions) as sessions
from ga
where date <= date+interval 3 day
group by link
But if I manually enter a date, it seems to work. Why is it not seeing date+interval 3 day as a proper date...?
Any help would be greatly appreciated! :)
Date is a column, not a value, you need to provide a specific date entry. Also "between" is a better keyword to use in this situation.
You need to also add date column in GROUP BY clause. Also, avoid using column name as date. It will create confusion.
Try below query :
select date_column,
link,
sum(sessions) as sessions
from ga
where date_column BETWEEN CURDATE()-3 AND CURDATE()
group by link, date_column

How to calculate date difference in mysql?

select sysdate-date('2016-12-02') from dual; --oracle
select now()-str_to_date('2016-12-02','%Y-%m-%d');-- mysql
Is there any way to get the day difference between two dates as number..??
You can use DATEDIFF with Strings or datefields, timestamps like this:
sample
SELECT DATEDIFF('2016-12-02 12:01:00', '2016_11_28 17:00:00') as cnt_days;
result
4
Another possible solution is:
SELECT TO_DAYS(date1)-TO_DAYS(date2) as cnt_days;
Hope it helps

MySQL - Query last six weeks of data

In order to calculate projected sales for a given day, I need to query the last six weeks of data for a given day. For example, if I want projected sales for Friday, I need to query data from the last six Fridays only.
I'm assuming there is a way to do this within a query, just not sure exactly how. Any help or insight would be greatly appreciated, as always.
Thanks in advance.
The easiest way is to use a limit.
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=6
ORDER BY date DESC LIMIT 6;
EDIT: To get this relative to today, just add CURDATE()
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=DAYOFWEEK(CURDATE())
ORDER BY date DESC LIMIT 6;
You can use a combination of different MySQL date and time functions to achieve this. Your query could look something like this:
SELECT fields FROM table WHERE DAYOFWEEK(table.date) = DAYOFWEEK(CURDATE()) ORDER BY table.date DESC LIMIT 6
Of course you can replace CURDATE() with the date that you are trying to predict.
Select * From table Where date_field > DATE_ADD(now(),INTERVAL -42 DAY)
That's about what you will need to do.
I just seen you wanted to query only a particular day of each week. Give me a moment and I'll update this.
Nevermind, I'm not going to edit this. Bobby has your answer for you. You just need to place variables in there through your script as needed. +1 Bobby.