[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL - Split range of dates from query into daily records

Posted on 2014-08-11
8
Medium Priority
?
361 Views
Last Modified: 2014-08-12
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
0
Comment
Question by:intimenordic
  • 4
  • 3
8 Comments
 
LVL 36

Expert Comment

by:ste5an
ID: 40252941
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
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40252945
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
 
LVL 36

Expert Comment

by:ste5an
ID: 40252975
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:intimenordic
ID: 40252986
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
 
LVL 36

Expert Comment

by:ste5an
ID: 40253052
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
 

Author Comment

by:intimenordic
ID: 40253187
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
 
LVL 36

Accepted Solution

by:
ste5an earned 2000 total points
ID: 40253260
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
 

Author Closing Comment

by:intimenordic
ID: 40255056
Thanks, spot on!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question