I am working in mySQL and I currently have a count of total orders by day, but I would like to add Saturday and Sunday orders to Monday then remove Saturday and Sunday values. I have done some research on this but I cannot seem to find anything similar to what I am trying to do.
My current data table looks like this:
Date | Daily Count
8-6-2020 25
8-7-2020 82
8-8-2020 24
8-9-2020 33
8-10-2020 18
8-11-2020 10
8-12-2020 25
8-13-2020 15
I need it to look something like this:
Date | Daily Count
8-6-2020 25
8-7-2020 82
8-10-2020 75
8-11-2020 10
8-12-2020 25
8-13-2020 15
In this one the Daily counts for the 8th and 9th are added to the 10th, then removed, because they are weekend days. Thank you in advance for your help!
Consider using a case expression to adjust the date:
select
case weekday(date)
when 5 then date + interval 2 day
when 6 then date + interval 1 day
else date
end as new_date,
sum(daily_count) as daily_count
from mytable
group by new_date
Related
I got a Table which looks like this:
DATE | Number
01-01-16 00:00:00 10
02-01-16 00:00:00 10
03-01-16 00:00:00 11
04-01-16 00:00:00 12
05-01-16 00:00:00 13
....
31-01-16 00:00.00 15
........
29-02-16 00:00:00 18
I got this table for the last few months.
I now want to retrieve the value of the rows, which contain the last day of the previous month and the month before the last month. So for today I would like to retrieve the Value of the 31-1-16 and 29-2-16.
My result should look like:
lastmonth | lastmonth2
18-> Corresponding value to Date: 29-02-16 | 15 -> value for 31-01-16
Would appreciate any help.
Cheers
Here is logic for the last day of this month and the previous month:
select last_day(curdate()) as last_day_of_this_month,
last_day(date_sub(curdate(), interval 1 month)) as last_day_of_prev_month
You can get the last day of any month relative to the current month by changing the "1".
And, I have no idea what date "30-2-16". When describing dates, you should use ISO standard formats. The last day of February 2016 was 2016-02-29.
This is Gordon's code for determining the correct dates plus subqueries to fetch the Number values for those rows:
SELECT
(SELECT Number FROM cc_open_csi_view
WHERE last_day(date_sub(curdate(), interval 1 month)) = date(`DATE`)) as lastmonth,
(SELECT Number FROM cc_open_csi_view
WHERE last_day(date_sub(curdate(), interval 2 month)) = date(`DATE`)) as lastmonth2
FROM DUAL;
Hope that's what you wanted! Works for me in a simple example. I don't know if you need the date() part around DATE but it seemed safest.
SELECT CASE
WHEN last_day(curdate()) = `DATE` THEN number
END as number_last_month,
CASE
WHEN last_day(date_sub(curdate(), interval 1 month)) = `DATE`
THEN number
END as number_last_month2
FROM cc_open_csi_view
I can't test it right now on sqlfiddle.
I'm trying to create a query to show the breakdown of leads relating to finance contracts grouped by hour.
Here's an example of what I have so far for hours:
SELECT CONCAT(HOUR(received), ':00-', HOUR(received)+1, ':00') AS Hours,
COUNT(*) as `leads`
FROM (digital_lead)
WHERE `received` BETWEEN '2014-11-01 00:00:00' AND '2014-11-24 00:00:00'
GROUP BY HOUR(received)
And here's the result ....
Hours usage
0:00-1:00 36
1:00-2:00 25
2:00-3:00 16
3:00-4:00 4
4:00-5:00 7
5:00-6:00 8
6:00-7:00 13 // etc all the way to 23:00 - 24:00
OK - so that seems to work, however, it aggregates all of the leads for that time slot over the course of the period set in the BETWEEN statement. I would like to be able to show the breakdown per hour for each day within the period range.
So that would look something like:
Date Hours leads
2014-11-01
0:00-1:00 36
1:00-2:00 25
2:00-3:00 16
3:00-4:00 4
4:00-5:00 7
5:00-6:00 8
6:00-7:00 13 // etc all the way to 23:00 - 24:00
So each date would be displayed and then the hours for that day and so forth.
Thanks.
Group by date as well:
SELECT DATE(received), HOUR(received), ...
...
GROUP BY DATE(received), HOUR(received)
Since you're grouping by hour() only, you'll essentialy be getting
Hour
1 sum of leads in hour 1 of jan 1, jan 2, jan 3, ... dec 31
2 sum of leads in hour 2 of jan 1, jan 2, jan 3, ... dec 31
Rather than
Jan 1 Hour 1 sum of leads for just that one hour on the one day
Jan 1 Hour 2 sum of leads for just that one hour on the one day
..
Dec 31 Hour 23 sum of leads
I need a query to get results from a table that has 2 columns
Column startdt (datetime), Column enddt (datetime)
there are some records with startdt 2013-07-19 and enddt 2013-07-29
I need to get the records with weekday = 1 (Tuesday)
the record with date 2013-07-19 is weekday 4 and ends 2013-07-29 which is 0
Actually i want to get the results that has for weekday Monday or another weekday.
You can check the above link for an example
http://sqlfiddle.com/#!2/a80ce/1
If you don't understand what i want to do let me explain. I have an event that starts July 15 and ends July 25. (Starts Monday and ends Thursday) The user selects one of the week days (Monday, Tuesday etc). If he select Tuesday then i want the query that will get all events that are active in Tuesday.
I already found the answer so if anyone want to check it
SELECT articleid,startdt,enddt,dayofweek(startdt), DATEDIFF(enddt,startdt) datedf
FROM events
WHERE (dayofweek(events.startdt) <= 3 AND dayofweek(events.enddt) >= 3)
OR DATEDIFF(enddt,startdt) >=6
(3 is the number of the weekday "Tuesday")
How about using the comments that other people gave you and use a query that combines both dayofweek and a simple greater/smaller/equal syntax as follows:
SELECT * FROM events where dayofweek(events.startdt) <= 6 AND dayofweek(events.enddt) >= 6
This gives the following results if the user specified a friday (= 6):
ARTICLEID STARTDT ENDDT
4 July, 12 2013 00:00:00+0000 July, 26 2013 00:00:00+0000
6 July, 16 2013 00:00:00+0000 July, 20 2013 00:00:00+0000
I do think that you are better of using dayofmonth however as this (maybe just to me) makes it clearer, possibly combining the use of both to ensure that it's active on a friday.
The OP indicates that events which are in the history should also be retrieved and as such the following query does what he wants:
SELECT * FROM events where dayofweek(events.startdt) <= 6 AND dayofweek(events.enddt) >= 6 OR DATEDIFF(enddt,startdt) >=6
How about this solution:
first you convert the day of week in format that 6 is Saturday and 7 is sunday(it's easier for me)
if(dayofweek(o.start_date) = 1, 7, dayofweek(o.start_date) -1)
after that you calc the days from the start_date needed to reach some of the weekdays
(7 - if(dayofweek(o.start_date) = 1, 7, dayofweek(o.start_date) -1)
finally you make sure that the difference in days between the two dates is no less that the above calculation
(7 - if(dayofweek(o.start_date) = 1, 7, dayofweek(o.start_date) -1) <= datediff(o.end_date, o.start_date)
I have some orders in a table and the last order date of 2011 is 20th Dec.
I'm using a sql command to calculate the number of orders in a given week:
SELECT CONVERT(VARCHAR(3),DATENAME(week,convert(datetime,order_date,103))) AS week,
COUNT(1) as orders
FROM order_table
where DATENAME(YEAR,convert(datetime,order_date,103)) = '2011'
GROUP BY CONVERT(VARCHAR(3),DATENAME(week,convert(datetime,order_date,103)))
order by week asc
It returns me the some of the following results:
Week | Orders
41 | 42
42 | 110
43 | 115
...
...
51 | 155
52 | 15
The trouble with this is is that the last order date of 2011 as mentioned that I have is 20th Dec 2011, which can't be week 52 so must be week 51.
I've got some other stats(off another system, not SQL server) which giving me some other figures and the last week on it is 51 which I have no doubt is correct. So there's going to be a queries if people are looking both!
Anyone have any idea or know how to sort this?
Thanks,
The iso_week of 20th Dec 2011 is 51. So maybe that is what you need.
SELECT datepart(iso_week, '2011-12-20')
SQL Counts a week as Sunday-Saturday, you can use the script below to see how the weeks break out for 2011. The 1st of January is a Saturday, which means the first week is only 1 day. There are 53 SQL weeks in 2011, and 53 weeks in most years
DECLARE #dStartDate DATETIME
SET #dStartDate = '01/01/2011'
WHILE #dStartDate < '01/01/2012'
BEGIN
PRINT CONVERT(VARCHAR, #dStartDate, 101)
+ ' : '
+ CONVERT(VARCHAR, DATEPART(WEEK, #dStartDate))
+ ' : '
+ DATENAME(DW, #dStartDate)
SET #dStartDate = #dStartDate + 1
END
If you want to change the day SQL counts as the first of the week, you can use the DATEFIRST command
http://msdn.microsoft.com/en-us/library/ms181598.aspx
SELECT DatePart(WEEK,order_date) AS WeekOfYear
FROM order_table
Should give you the week number.
So do
Select * from order_table Where DatePart(Week, order_date) = 52
See what is week 52 not what you think is 52.
PS Given you are starting week 1 on the 1/1 it's not possible to have a 52 week year, unless 1 and or 52 are not seven day weeks.
I have the following (MySQL) table called "tweets":
tweet_id created_at
---------------------
1 1298027046
2 1298027100
5 1298477008
I want MySQL returning the number of tweets per day of the week; taking the above data it should return:
Sunday 1
Monday 2
I now have the following query to accomplish this:
SELECT COUNT(tweet_id) AS tweets, DAYNAME(FROM_UNIXTIME(`created_at`)) AS day
FROM tweets
GROUP BY day
ORDER BY DAYOFWEEK(day)
This however returns a list that starts with Wednesday:
Wednesday 2019
Tuesday 2072
Monday 932
Sunday 1433
Saturday 4321
Friday 643
Thursday 1542
How is this list ordered? Why not just Sun to Sat and how * can * I accomplish that?
DAYOFWEEK takes a date ('2011-02-26'), but you're giving it a day name ('Saturday').
Try to change the order clause like this
order by dayofweek(FROM_UNIXTIME(`created_at`))