SQL - Split range of dates from query into daily records

Hi Experts.

This SQL
  
select p.personid, p.PERSONNUM, p.FULLNM, ri.STARTDTM, ri.ENDDTM, rs.REQUESTSTATUSNM , ri.requestitemid
  from REQUESTITEM ri
  left outer join PERSON p on p.PERSONID=ri.EMPLOYEEID
  left outer join REQUESTSTATUS rs on rs.REQUESTSTATUSID = ri.REQUESTSTATUSID
  where rs.REQUESTSTATUSNM='SUBMITTED'

Open in new window


gives this result:
202	700002	Kjærstad, Jon	2014-08-11 00:00:00.000	2014-08-15 00:00:00.000	SUBMITTED	152
202	700002	Kjærstad, Jon	2014-09-07 00:00:00.000	2014-09-10 00:00:00.000	SUBMITTED	302

Open in new window


I need to split this into daily records like this:

202	700002	Kjærstad, Jon	2014-08-11 00:00:00.000	2014-08-11 00:00:00.000	SUBMITTED	302
202	700002	Kjærstad, Jon	2014-08-12 00:00:00.000	2014-08-12 00:00:00.000	SUBMITTED	302
202	700002	Kjærstad, Jon	2014-08-13 00:00:00.000	2014-08-13 00:00:00.000	SUBMITTED	302
202	700002	Kjærstad, Jon	2014-08-14 00:00:00.000	2014-08-14 00:00:00.000	SUBMITTED	302
202	700002	Kjærstad, Jon	2014-08-15 00:00:00.000	2014-08-15 00:00:00.000	SUBMITTED	302
202	700002	Kjærstad, Jon	2014-09-07 00:00:00.000	2014-09-07 00:00:00.000	SUBMITTED	304
202	700002	Kjærstad, Jon	2014-09-08 00:00:00.000	2014-09-08 00:00:00.000	SUBMITTED	304
202	700002	Kjærstad, Jon	2014-09-09 00:00:00.000	2014-09-09 00:00:00.000	SUBMITTED	304
202	700002	Kjærstad, Jon	2014-09-10 00:00:00.000	2014-09-10 00:00:00.000	SUBMITTED	304

Open in new window


Any thoughts on how to achieve this?

Thanks,
Bjorn
intimenordicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Well, JOIN it with a tally table. E.g.
DECLARE @ToSplit TABLE ( [Date] DATE );

INSERT  INTO @ToSplit
        ( [Date] )
VALUES  ( '20140811' ),
        ( '20140907' );

SELECT  TS.[Date] ,
        Tally.N ,
        DATEADD(DAY, Tally.n - 1, TS.[Date])
FROM    @ToSplit TS
        CROSS JOIN ( VALUES ( 1), ( 2), ( 3), ( 4), ( 5) ) Tally ( n );

Open in new window

0
Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

You can do like this

;WITH CTE AS
(
select p.personid, p.PERSONNUM, p.FULLNM, ri.STARTDTM, ri.ENDDTM, rs.REQUESTSTATUSNM , ri.requestitemid
  from REQUESTITEM ri
  left outer join PERSON p on p.PERSONID=ri.EMPLOYEEID
  left outer join REQUESTSTATUS rs on rs.REQUESTSTATUSID = ri.REQUESTSTATUSID
  where rs.REQUESTSTATUSNM='SUBMITTED'
),CTE2 as
(
SELECT personid, PERSONNUM, FULLNM, STARTDTM, ENDDTM, REQUESTSTATUSNM , requestitemid FROM CTE
UNION ALL
SELECT personid, PERSONNUM, FULLNM, DATEADD(D,1,STARTDTM) STARTDTM, ENDDTM, REQUESTSTATUSNM , requestitemid FROM CTE WHERE DATEADD(D,1,STARTDTM) <= ENDDTM
)
SELECT * FROM CTE2 OPTION (MAXRECURSION 0)

Open in new window

0
ste5anSenior DeveloperCommented:
I see, haven't seen the start-end valus in the souce:
DECLARE @ToSplit TABLE
    (
      DateFrom DATE ,
      DateTo DATE
    );

INSERT  INTO @ToSplit
VALUES  ( '20140811', '20140815' ),
        ( '20140907', '20140910' );

SELECT  TS.DateFrom ,
        TS.DateTo ,
        Tally.N ,
        DATEADD(DAY, Tally.n - 1, TS.DateFrom)
FROM    @ToSplit TS
        CROSS JOIN ( VALUES ( 1), ( 2), ( 3), ( 4), ( 5) ) Tally ( n )
WHERE   Tally.n - 1 <= DATEDIFF(DAY, DateFrom, DateTo);

Open in new window


When your date ranges are larger, use a materialized tally table.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

intimenordicAuthor Commented:
Thanks for getting back!

Ste5an; I'm afraid I do not quite understand how to join my query with a tally table. The full dataset will most likely contain hundreds/thousands of records, while this is only a very simplified example.
I guess I could do a select into @ToSplit from my query, but still I can't grasp how to bundle it all together.

Vikas Garg: Your example seems to be a start, but gives me only two lines per request id like this:
202	700002	Kjærstad, Jon	2014-08-11 00:00:00.000	2014-08-17 00:00:00.000	SUBMITTED	302
202	700002	Kjærstad, Jon	2014-08-12 00:00:00.000	2014-08-17 00:00:00.000	SUBMITTED	302
202	700002	Kjærstad, Jon	2014-09-07 00:00:00.000	2014-09-20 00:00:00.000	SUBMITTED	304
202	700002	Kjærstad, Jon	2014-09-08 00:00:00.000	2014-09-20 00:00:00.000	SUBMITTED	304

Open in new window

0
ste5anSenior DeveloperCommented:
The @ToSplit in my sample is your table.. as your example wasn't complete, I had to build my own table to fiddle with.
0
intimenordicAuthor Commented:
Ste5an: Thanks for clarifying. I do a select into the temp tally table and it works!

However, I am wondering about
CROSS JOIN ( VALUES ( 1), ( 2), ( 3), ( 4), ( 5),(6),(7),(8),(9) ) Tally ( n )

Open in new window

I added 7,8,9 for testing,

Is it possible to cross join all values without setting a max? Requests could be from 2014-01-01 to 2014-01-31, but this query will max return 9 records per query.

Thanks,
Bjorn
0
ste5anSenior DeveloperCommented:
Then that's the point, where we should use a materilzed tally table, thus a real table instead of a table-value constructor:
 
USE master
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE dbo.NumberTally
    (
      n INT NOT NULL ,
      CONSTRAINT PK_NumberTally PRIMARY KEY CLUSTERED ( n ASC )
    );
GO

DECLARE @n INT = 1;

WHILE ( @n <= 1000000 )
    BEGIN
		INSERT INTO dbo.NumberTally (n) VALUES ( @n);
        SET @n += 1;
    END;
GO

Open in new window


with

DECLARE @ToSplit TABLE
    (
      DateFrom DATE ,
      DateTo DATE
    );

INSERT  INTO @ToSplit
VALUES  ( '20140811', '20140815' ),
        ( '20140907', '20140910' );

SELECT  TS.DateFrom ,
        TS.DateTo ,
        NT.N ,
        DATEADD(DAY, NT.n - 1, TS.DateFrom)
FROM    @ToSplit TS
        CROSS JOIN master.dbo.NumberTally NT
WHERE   NT.n  <= DATEDIFF(DAY, DateFrom, DateTo) + 1;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intimenordicAuthor Commented:
Thanks, spot on!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.