how to make sql group by week total - sql-server

sql server group by week sum
I have below code, time table.
below are time table columns
below is user table columns
userid,firstname,lastname
below are records of time table
timeid,shiftdate,starttime,endtime,userid
1,1st Jun 2019,1:00,10:00,1
2,2st Jun 2019,1:00,10:00,2
3,3rd Jun 2019,4:00,11:00,1
10,11th Jun 2019,4:00,11:00,1
14,11th Jun 2019,4:00,11:00,1
19,11th Jun 2019,4:00,11:00,1
my question is I want to generate weekly total report
like below e.g.
Userid | 3rd June to 9th June | 10th to 16 June
1 10 20
2 5 5
I have tried below code
select sum(datediff(hour,startdate,enddate),userid,shiftdate from time
group by userid,shiftdate
my it is giving me total of daily with respect to user. but what i need
I ned to have sum of hours between first day of week to last day of each week.
my it is giving me total of daily with respect to user. but what i need
I ned to have sum of hours between first day of week to last day of each week.
can you help on it

This may help
DECLARE #TimeTable TABLE(
timeid INT,shiftdate DATETIME ,starttime TIME,endtime TIME ,userid INT
)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(1,'1 Jun 2019','1:00','10:00',1)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(2,'2 Jun 2019','1:00','10:00',2)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(2,'3 Jun 2019','1:00','10:00',2)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(3,'3 Jun 2019','4:00','11:00',1)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(10,'11 Jun 2019','4:00','11:00',1)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(14,'11 Jun 2019','4:00','11:00',3)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(19,'11 Jun 2019','4:00','11:00',3)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(2,'11 Jun 2019','1:00','10:00',2)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(10,'19 Jun 2019','4:00','11:00',1)
INSERT INTO #TimeTable(timeid,shiftdate,starttime,endtime,userid) VALUES(10,'20 Jun 2019','4:00','11:00',1)
;WITH WeeklyTotal
AS(
SELECT *
,DATEDIFF(HOUR,starttime,endtime) totalHours
,DATEPART(WEEK, shiftdate) - DATEPART(WEEK, DATEADD(MM, DATEDIFF(MM,0,shiftdate), 0))+ 1 AS weekOfMonth
FROM #TimeTable
)
SELECT * FROM (
SELECT userid,weekOfMonth,totalHours
FROM WeeklyTotal
) aaa
pivot
(
SUM(totalHours)
for weekOfMonth in ([1], [2], [3],[4],[5],[6])
) piv;

Related

Get last 6 months monthname.month number and Years in simple select statement

How to get last 6 months month name, month number and Years in simple select statement in sqlserver .
The no of months is 6, and is fixed
12 Dec 2015
11 Nov 2015
10 Oct 2015
9 Sep 2015
8 Aug 2015
7 Jul 2015
6 Jun 2015
This should handle year end boundaries
say, if the current month is Feb 2016, the result should give 2015 months.
2 Feb 2016
1 Jan 2016
12 Dec 2015
11 Nov 2015
10 Oct 2015
9 Sep 2015
8 Aug 2015
You can do it with the following:
SELECT MONTH(DATEADD(mm, -m, GETDATE())) AS m,
LEFT(DATENAME(mm, DATEADD(mm, -m, GETDATE())), 3) AS n,
YEAR(DATEADD(mm, -m, GETDATE())) AS y
FROM (VALUES (0),(1),(2),(3),(4),(5),(6)) t(m)
Output:
m n y
12 Dec 2015
11 Nov 2015
10 Oct 2015
9 Sep 2015
8 Aug 2015
7 Jul 2015
6 Jun 2015
Try this
;with cte as
(
select 0 as num
union all
select num+1 from cte where num<6
)
select month(dates),datename(month,dates),year(dates)
from
(
select dateadd(mm,-num,datadd(dd,1,eomonth(getdate(),-1))) as dates
from cte
) A
SQL FIDDLE DEMO
select datepart(m,GETDATE()) MonthNumber,left(datename(month,GETDATE()),3) as Month,year(GETDATE()) as Year union all
select datepart(m,DATEADD(month,-1,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-1,GETDATE())),3) as Month,year(DATEADD(month,-1,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-2,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-2,GETDATE())),3) as Month,year(DATEADD(month,-2,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-3,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-3,GETDATE())),3) as Month,year(DATEADD(month,-3,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-4,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-4,GETDATE())),3) as Month,year(DATEADD(month,-4,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-5,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-5,GETDATE())),3) as Month,year(DATEADD(month,-5,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-6,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-6,GETDATE())),3) as Month,year(DATEADD(month,-6,GETDATE())) as Year
The above query will work for most of the RDBMS.
For SQL Server specific use the below query.
SELECT MONTH(DATEADD(month, -month, GETDATE())) AS MonthNumber ,
LEFT(DATENAME(MONTH, DATEADD(month, -month, GETDATE())), 3) AS MonthName,
YEAR(DATEADD(month, -month, GETDATE())) AS Year
FROM ( VALUES (0), (1), (2), (3), (4), (5),(6) ) t ( month )
Try DATEADD:
Select * FROM Table where YourDate>=DATEADD(m, -6, GETDATE())

get total orders for the last 1 month every week

I have an "Orders" table containing column CreatedDate (datetime) on when the order took place.
Today is Feb 13, 2014.
How to get total records for the last 4 weeks for every week.
it should return something like this:
PerDate Total
2014-01-26 13 <--- sunday
2014-02-02 24 <--- sunday
2014-02-09 33 <--- sunday
2014-02-13 35 <--- this is today
13 from the first record is the total record from 2014-01-20 00:00:00 AM (monday) to 2014-01-26 12:00:00 PM (sunday)
24 from the 2nd record is the total record from 2014-01-27 00:00:00 AM (monday) to 2014-02-02 12:00:00 PM (sunday)
33 from the 3rd record is the total record from 2014-02-03 00:00:00 AM (monday) to 2014-02-09 12:00:00 PM (sunday)
35 from the 4th record is the total record from 2014-02-10 00:00:00 AM (monday) to 2014-02-13 (today)
so they are in Ascending order.
You should use DATEPART(dw,CreatedDate) function to calculate date of begin and end of the week for each CreatedDate and then just group by this field:
WITH T AS
(
SELECT
cast(floor(cast(CreatedDate as float)) as datetime)
-DATEPART(dw,CreatedDate)+1 as BeginOfweek,
cast(floor(cast(CreatedDate as float)) as datetime)
-DATEPART(dw,CreatedDate)+8 as EndOfWeek
FROM ORDERS
WHERE cast(floor(cast(CreatedDate as float)) as datetime) BETWEEN
DATEADD(WEEK,-4,GETDATE())
AND
GETDATE()
)
SELECT
BeginOfWeek,
MIN(CASE WHEN GETDATE()<EndOfWeek
THEN GETDATE()
ELSE EndOfWeek
END) as EndOfWeek,
Count(*) as OrdersCount
FROM T
Group by BeginOfWeek
SQLFiddle demo

yearweek on SQL server

How can I extract the year and week combination from a date in SQL Server using T-SQL and have it match the MySQL yearweek result?
For example these MySQL queries (Click here for MySQL SQL Fiddle):
SELECT yearweek('2013-12-31');
SELECT yearweek('2014-01-01');
This returns 201352 for both dates. That is the expected result.
But in SQL Server...
datepart works as expected for the year extract
sometimes datepart does not return the expected value for iso_week
The MySQL result cannot be achieved with this T-SQL query...
SELECT datepart(year, #dt) * 100 + datepart (iso_week, #dt);
T-SQL versions of the MySQL queries above (Click here for T-SQL SQL Fiddle):
SELECT datepart(year, '2013-12-31') * 100 + datepart (iso_week, '2013-12-31');
SELECT datepart(year, '2014-01-01') * 100 + datepart (iso_week, '2014-01-01');
The result is 201352 for the first date and 201401 for the second date.
However, this is not the expected result because...
2014-01-01 belongs to the last week of 2013
So the expected result is 201352
Do any of you more experienced T-SQL developers know how to extract the year/week of a given date and have this match what I see in MySQL?
I need to have the week start on Monday. This is why I am using iso_week. I have tested the results with week anyway and found the same issue. This query also produces 201401 when 201352 is expected.
SELECT datepart(year, '2014-01-01') * 100 + datepart (week, '2014-01-01');
If you look at the ISO_Week datepart definition at http://msdn.microsoft.com/en-us/library/ms174420.aspx, you'll see the following:
ISO 8601 includes the ISO week-date system, a numbering system for
weeks. Each week is associated with the year in which Thursday
occurs...
The numbering system in different countries/regions might not comply
with the ISO standard.
Since January 1, 2014 was a Wednesday; January 2, 2014 was the first Thursday of the year and thus week 1 of 2014 (according to ISO 8601).
Furthermore, looking at the MySQL definitions for yearweek and week, there are several mode options (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week)
So, I think you're going to have to write your own yearweek function for the week counting rule you want:
CREATE FUNCTION dbo.yearweek(#date date)
RETURNS INT
as
begin
set #date = dateadd(dd,-datepart(dw,#date)+1, #date)
return datepart(year,#date)*100 + datepart(week,#date)
end
go
select dbo.yearweek('2013-12-31'), dbo.yearweek('2014-01-01')
NOTE: I haven't fully tested this code, and I'm not sure exactly what your requirements are. This is just meant as an example of the type of process you need to follow.
I think it's because you're using "ISO_WEEK" as the datepart
Look at the description under the "ISO_WEEK datepart" here: http://msdn.microsoft.com/en-us/library/ms174420.aspx
using "WEEK" instead might get the desired result
TO extract YEAR, MONTH or DAY you can simply use the YEAR, MONTH and DAY function as shown in the followinf example. for any other part you have to use the DATEPART function , read here for a detailed list of arguements you can pass to DATEPART function.
SELECT YEAR(GETDATE()) AS [YEAR]
, DATEPART(WEEK, GETDATE()) [Week]
UPDATE
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) +'-'
+ CAST(DATEPART(WEEK, GETDATE()) AS VARCHAR(2))
It seems to me that what you want is a week number based on the previous Monday. Is this correct? If so, iso_week is no use to you as it is based on this week's Thursday.
To get the year and week based on the previous Monday, I think you need something like the following.
set datefirst 1 ;
select
SomeDate ,
datename( weekday, SomeDate ) as [WeekdayName] ,
datepart( weekday, SomeDate ) as [Weekday] ,
datepart( week, SomeDate ) as [Week] ,
datepart( iso_week, SomeDate ) as [ISO_week] ,
dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) as [PreviousMonday] ,
datepart( year, dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) ) as [MondayYear] ,
datepart( week, dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) ) as [MondayWeek]
from
dbo.DateDemo
order by
SomeDate ;
Abbreviated sample output is as follows.
SomeDate PreviousMonday MondayYear MondayWeek
2013-12-25 2013-12-23 2013 52
2013-12-26 2013-12-23 2013 52
2013-12-27 2013-12-23 2013 52
2013-12-28 2013-12-23 2013 52
2013-12-29 2013-12-23 2013 52
2013-12-30 2013-12-30 2013 53
2013-12-31 2013-12-30 2013 53
2014-01-01 2013-12-30 2013 53
2014-01-02 2013-12-30 2013 53
2014-01-03 2013-12-30 2013 53
2014-01-04 2013-12-30 2013 53
2014-01-05 2013-12-30 2013 53
2014-01-06 2014-01-06 2014 2

Get all quarters in Month, Year format between two dates

I have two dates say '2011-01-23' and '2015-11-29',
'2011-01-23' falls in first quarter of 2011 so 'Jan 2011'
'2015-11-29' falls in fourth quarter of 2015 so 'Oct 2015'
In SQL Server I want get all quarters in a select list.
e.g.
Input: #StartDate='2011-01-23' , #EndDate='2015-11-29'
Output:
Jan 2011
Apr 2011
Jul 2011
Oct 2011
Jan 2012
Apr 2012
Jul 2013
Oct 2013
Jan 2014
......
......
......
Jul 2015
Oct 2015
You can use a recursive CTE to generate the dates as follows:
declare #StartDate datetime
declare #EndDate datetime
select #StartDate='2011-01-23' , #EndDate='2015-11-29'
;With Quarters as (
select DATEADD(quarter,DATEDIFF(quarter,0,#StartDate),0) as dt
union all
select DATEADD(quarter,1,dt)
from Quarters
where dt < DATEADD(quarter,DATEDIFF(quarter,0,#EndDate),0)
)
select
--I'd usually keep them as dates at this point, but to match your requirement
CONVERT(varchar(3),dt,109) + ' ' + CONVERT(varchar(4),dt,120)
from Quarters order by dt
This also uses a couple of other tricks - it uses CONVERT with far too short target datatypes to quickly truncate the strings to just the parts that we want to keep - and it uses a DATEADD/DATEDIFF pair to quickly round a datetime value down to it's nearest interval boundary.
For SQL Server 2012, you could instead use FORMAT to produce the output string, but I've not experimented with that much so I'll leave that as an exercise...
Finally, I found the solution of my question..
WITH mycte AS
(
SELECT CAST('2011-01-01' AS DATE) DateValue
UNION ALL
SELECT DATEADD(Q,1,DateValue) FROM mycte WHERE DATEADD(Q,1,DateValue) < '2012-12-31'
)
SELECT CONVERT(varchar(3), DATENAME(MONTH,DateValue))+ ' ' +Convert(varchar(4),DATEPART(YYYY,DateValue)) FROM mycte OPTION (MAXRECURSION 0)

How to calculate totals by week start and end dates based on date column in the table?

my sql server table named timesheet has 4 columns
employee_id int
task_start_date datetime
task_end_date datetime
hours_worked int
I need to derive week start date and week end date and total hours the employee has worked in that week. My resulting dataset should have 4 columns -
Week_Start_Date Week_End_Date Employee_Id Total_Hours_Charged
In other words, i need to get total hours charged by each employee for all the weeks from that table. Could anyone please help me write a query for that?
SQL Server 2005 and above
Assumptions:
Task start and end date values are always same.
Week start and end dates are calculated based on the task_start_date.
You can use the DATEPART function to find the day of the week for a given date and then use DATEADD function to provide the output from DATEPART as input to calculate the first and last days of the week.
Click here to view the demo in SQL Fiddle.
Script:
The script calculates the week totals hours assuming that week begins on Sunday and ends on Saturday.
CREATE TABLE timesheet
(
employee_id int
, task_start_date datetime
, task_end_date datetime
, hours_worked int
);
INSERT INTO timesheet
(employee_id, task_start_date, task_end_date, hours_worked)
VALUES
(1, '20120331', '20120331', 6),
(1, '20120401', '20120401', 3),
(1, '20120403', '20120403', 8),
(1, '20120409', '20120409', 5),
(1, '20120412', '20120412', 4),
(2, '20120402', '20120402', 7),
(2, '20120403', '20120403', 6),
(3, '20120409', '20120409', 4),
(1, '20120412', '20120412', 8);
;WITH empworkhours AS
(
SELECT DATEADD(DAY
, -(DATEPART(dw, task_start_date) -1)
, task_start_date) AS week_start
, DATEADD(DAY
, 7 - (DATEPART(dw, task_start_date))
, task_start_date) AS week_end
, employee_id
, hours_worked
FROM timesheet
)
SELECT week_start
, week_end
, employee_id
, SUM(hours_worked) total_hrs_per_week
FROM empworkhours
GROUP BY week_start
, week_end
, employee_id;
Output:
WEEK_START WEEK_END EMPLOYEE_ID TOTAL_HRS_PER_WEEK
-------------- -------------- ----------- -----------------
March, 25 2012 March, 31 2012 1 6
April, 01 2012 April, 07 2012 1 11
April, 01 2012 April, 07 2012 2 13
April, 08 2012 April, 14 2012 1 17
April, 08 2012 April, 14 2012 3 4

Resources