I am making a report in SSRS and I need to show two dynamic columns
YYYY week 1, YYYY week 2..YYYY week X
week 1 week 2 ...week X
The problem is that in SSRS when executing the report they are shown as follows
example:
YYYY week 1
YYYY week 2
week 1
week 2
but I need them to show
YYYY week 1
week 1
YYYY week 2
week 2
Related
My company’s week begins on a Sunday.
The issue I’m having is extracting the week number from the date in the database.
For example,02/01/2022 falls in week 52 of 2021 using the Sunday week beginning logic. How can I write a SQL code to give me :
a) the week number (I.e 2nd Jan 2022 is part of week 52)
b) the correct year of the week. i.e 2nd Jan 2022 , I want it to bring back the year “2021” so my data set is complete and accurate.
N.b.
Database name I’m using is dw-fin and column of the date is called date-created
You can use the WEEK function with a 'mode'.
Which is an integer indicating the starting of the week.
And the YEARWEEK function also has a mode.
Mode
First day of week
Range
Week 1 is the first week
0
Sunday
0-53
with a Sunday in this year
1
Monday
0-53
with 4 or more days this year
2
Sunday
1-53
with a Sunday in this year
3
Monday
1-53
with 4 or more days this year
4
Sunday
0-53
with 4 or more days this year
5
Monday
0-53
with a Monday in this year
6
Sunday
1-53
with 4 or more days this year
7
Monday
1-53
with a Monday in this year
Example :
select date_column
, month(date_column) as month
, year(date_column) as year
, yearweek(date_column, 2) as mode2_yearweek
, floor(yearweek(date_column, 2)/100) as mode2_year
, week(date_column, 2) as mode2_week
from (select date('2022-01-01') as date_column) q;
date_column
month
year
mode2_yearweek
mode2_year
mode2_week
2022-01-01
1
2022
202152
2021
52
Demo on db<>fiddle here
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
I need stats on orders, week by week, so I have done this:
SELECT YEAR(orders.date), WEEKOFYEAR(orders.date), COUNT(*)
FROM orders
GROUP BY YEAR(orders.date), WEEKOFYEAR(orders.date)
It worked for one year, but just now (new year) it does not count the last days of 53rd week (jan 1st, 2nd, 3rd). How can I update my Query in order to have the full last week (from Monday 2015-12-28 to Sunday 2016-01-03)?
You need to switch to YEARWEEK(orders.date,3) to get the ISO weeks as a single column. Using WEEK(orders.date,3) (which is exactly the same as WEEKOFYEAR) will return the correct week number, but YEAR(orders.date) will return either 2015 or 2016, splitting the week into four days in 2015 and and three days in 2016.
As Strawberry mentioned in the comments you're looking for the WEEK function. I just checked the documentation at the MySQL website.
Week(date [,mode])
This function returns the week number for date. The two-argument form of WEEK() enables you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If the mode argument is omitted, the value of the default_week_format system variable is used
Here's an example
SELECT WEEK('2008-12-31',1);
=> 53
It should also be noted that this is not the same as the WEEKOFYEAR function.
Returns the calendar week of the date as a number in the range from 1 to 53. WEEKOFYEAR() is a compatibility function that is equivalent to WEEK(date,3).
We can see that the value of the mode parameter here is 3. Here is the table that shows the values for the modes
Mode First day of week Range Week 1 is the first week
0 Sunday 0-53 With a Sunday in this year
1 Monday 0-53 With 4 or more days this year
2 Sunday 1-53 With a Sunday in this year
3 Monday 1-53 With 4 or more days this year
4 Sunday 0-53 With a Sunday in this year
5 Monday 0-53 With 4 or more days this year
6 Sunday 1-53 With a Sunday in this year
7 Monday 1-53 With 4 or more days this year
Source
https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week
As I understand you, you want calculate total orders for one week only once not twice for various years.
I've written SQL query for SQL Server, but I think you can easy rewrite it on mysql.
DECLARE #test_table TABLE(created_date DATETIME, VALUE INT)
INSERT INTO #test_table
SELECT N'20151231', 10
UNION ALL
SELECT N'20151228', 20
UNION ALL
SELECT N'20160101', 40
UNION ALL
SELECT N'20160104', 5
UNION ALL
SELECT N'20160107', 2
SELECT first_day_week_number,
SUM(value) AS total
FROM
(SELECT *,
DATEPART(ww,
DATEADD(dd, -DATEPART(dw, created_date)+2, created_date)) AS first_day_week_number
FROM #test_table ) AS T
GROUP BY first_day_week_number
To avoid problems with years you could calculate first week day of order date and then get week number from it.
I am facing one issue while fetching the week number for 2016-01-01(fist week) in mysql. it return 53 if i use week() function or weekofyear() function . I have to get as 1 since I am linking the weeknumber in my project in other places and if it returns 1 the next week number also should be adjusted according to that. Kindly any one help me on this.
The manual says:
If the week containing January 1 has 4 or more days in the new year,
it is week 1.
Otherwise, it is the last week of the previous year, and the next week
is week 1.
So you can provide the mode to the week function.
Mode First day of week Range Week 1 is the first week …
0 Sunday 0-53 with a Sunday in this year
1 Monday 0-53 with 4 or more days this year
2 Sunday 1-53 with a Sunday in this year
3 Monday 1-53 with 4 or more days this year
4 Sunday 0-53 with 4 or more days this year
5 Monday 0-53 with a Monday in this year
6 Sunday 1-53 with 4 or more days this year
7 Monday 1-53 with a Monday in this year
Something like this:
select WEEK('2016-01-01',0) + 1;
FIDDLE DEMO
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)