Author |
Topic |
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2015-01-30 : 17:19:45
|
I need to build maintanance calender, The maintanance will be from monday - sunday. declare @users(UserID int, Name varchar(50))insert into @usersselect 1,'Member1' union allselect 2,'Member2' union allselect 3,'Member3' union allselect 4,'Member4' union allselect 5,'Member5' union allselect 6,'Member6' Lets assume the maintanance start on 02/02/2015.Sample Output:select 'Member1' as Member,'02/02/2015 - 02/08/2015' as Support1,'03/16/2015 - 03/22/2015' as support2,'04/27/2015 - 05/03/2015' as support3.....select 'Member2' as Member,'02/09/2015 - 02/15/2015' as Support1,'03/23/2015 - 03/29/2015' as support2,'05/04/2015 - 05/10/2015' as support3..... I need to build the calender till dec 31st 0215. Also, i need to build for the 6 members sample row wise data : 2/2/2015 - 2/8/2015 member12/9/2015 - 2/15/2015 member22/16/2015 - 2/22/2015 member32/23/2015-3/01/2015 member43/02/2015 - 3/08/2015 member53/09/2015 - 3/15/2015 member63/16/2015 - 3/22/2015 member13/23/2015 - 3/29/2015 member2..... also i need this data as column wiseany sample query please |
|
djj55
Constraint Violating Yak Guru
352 Posts |
Posted - 2015-02-02 : 14:53:58
|
This is not the answer but it might help you declare @users table (UserID int, Name varchar(50));insert into @usersselect 1,'Member1' union allselect 2,'Member2' union allselect 3,'Member3' union allselect 4,'Member4' union allselect 5,'Member5' union allselect 6,'Member6';with dates (Mon, Sun) AS (select DATEADD(day, 7*(T.Nval-1), '2/2/2015'), DATEADD(day, 6*T.Nval, '2/2/2015') from Temp_DJJ.dbo.NumberTable TWHERE DATEADD(day, 7*T.Nval, '2/2/2015') < '12/31/2015')select U.Name, D.Mon, D.Sunfrom dates Dcross apply @users U; djj |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2015-02-03 : 05:29:47
|
[code]declare @users TABLE (UserID int, Name varchar(50))insert into @usersselect 1,'Member1' union allselect 2,'Member2' union allselect 3,'Member3' union allselect 4,'Member4' union allselect 5,'Member5' union allselect 6,'Member6'DECLARE @StartDate DATETIME = '20150202', @EndDate DATETIME = '20151231', @Items INT = (SELECT COUNT(*) FROM @Users);-- SwePesoWITH cteDates(UserID, StartDate, EndDate)AS ( SELECT 0 AS UserID, DATEADD(DAY, DATEDIFF(DAY, '19000101', @StartDate), '19000101') AS StartDate, DATEADD(DAY, DATEDIFF(DAY, '19000101', @StartDate), '19000107') AS EndDate UNION ALL SELECT CASE WHEN UserID >= @Items - 1 THEN 0 ELSE d.UserID + 1 END AS UserID, DATEADD(DAY, 7, d.StartDate) AS StartDate, DATEADD(DAY, 7, d.EndDate) AS EndDate FROM cteDates AS d WHERE DATEADD(DAY, 7, d.StartDate) < @EndDate)SELECT d.StartDate, CASE WHEN d.EndDate > @EndDate THEN @EndDate ELSE d.EndDate END AS EndDate, u.NameFROM cteDates AS dINNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY UserID) - 1 AS UserID, Name FROM @Users ) AS u ON u.UserID = d.UserIDORDER BY d.StartDate;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2015-02-03 : 07:28:34
|
Hi Peso,Thank you for your nice sample. worked. |
|
|
|
|
|