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.
Related
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.
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
In my database, there is a field called birthday(date) and I want to retrieve records that their birth month and birth day is equal to the current month and the day. Is there a way to write a query for this? Or I just have to do it by retrieving all of the records and find the matching records after by using another program? Thanks!
This might also work: (NOTE that I am not sure if you mean day within a Month or day within a week)?
SELECT * FROM TABLE
WHERE MONTH(birthday) = MONTH(CURRENT_DATE)
AND DAY(birthday) = DAY(CURRENT_DATE) --assuming day within a month
select * from table
where date_format(birthday, '%m%d')=date_format(current_date, '%m%d');
The above query would not make use of mysql index
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;
select (SELECT power FROM newdb.newmeter where date(dt)=curdate() order by dt desc limit 1)
-(select Power from newdb.newmeter where date(dt)=(select date(subdate(now(), interval weekday(now()) day))) limit 0,1) as difference;
The above query is part of my prog which gives me difference in data being stored from day 1 of the week to the current day of the week. Those queries individually works fine as below, and returns:
SELECT power FROM newdb.newmeter where date(dt)=curdate() order by dt desc limit 1;
result: 941690 current time
select Power from newdb.newmeter where date(dt)=(select date(subdate(now(), interval weekday(now()) day))) limit 0,1;
result 93242.4 at the start of the week (or day for today as its monday)
But as soon as I run the difference query which is just the difference between above two that result in : 848447.8515625
This seems just really strange don't understand whats wrong with it? Please help.
You don't order by dt in your second query, looking for power at the start of week. Which means you are selecting undefined record that happens to have matching date. For a simple select the table is usually sorted by insert order, but it can change when optimizer thinks it can run a query faster using some other order. Basically, if you don't define order you don't care about order.
How many decimal places do you want in the answer? Use DECIMAL(7,1) for Power (I assume you want one decimal place) instead and see what you get.
I tend to avoid floats/doubles as the approximate values can get you into trouble.