Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

select multirecs from single recs without cursor Expand / Collapse
Author
Message
Posted Friday, September 10, 2010 2:44 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 7, 2012 2:26 AM
Points: 24, Visits: 93
Wondering if there's a way to do a select query without a cursor (although easy enough to do, curious if can be done without one) to do something like this... here's a small scale sample of what I'm trying to do with a select query...

Sample recs in PublicHols table:
26Jan2010,26Jan2010 -- = 1 day
25Apr2010,25Apr2010 -- = 1 day
25Dec2010,26Dec2010 -- = 2 days

Sample results expected:
26Jan2010,1
25Apr2010,1
25Dec2010,1
26Dec2010,1

Sample results at moment without cursor:
26Jan2010,1
25Apr2010,1
25Dec2010,2 -- want this split into 2 records instead

At moment I'm using this but it can return days > 1, so considering cursor to select them broken up into single records:
select
StartDate,
cast(EndDate-StartDate as integer)+1 as Days
from PublicHols

SQL Server 2000 (& 2005)
Post #983603
Posted Friday, September 10, 2010 2:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:52 AM
Points: 6,872, Visits: 14,185
This couldn't have come at a better time, Wayne & I had something similar this week. Can you post the ddl for the table, along with a few INSERTs to get some sample data into it?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #983613
Posted Friday, September 10, 2010 11:57 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 7, 2012 2:26 AM
Points: 24, Visits: 93
No problem thnx for the posting tip, good idea, I'll do that shortly...
however got this answer from another forum & it works perfectly. . :)

select dateadd(dd, n.number, h.StartDate)
from master..spt_values n
join PublicHols h on n.type = 'P'
and dateadd(dd,n.number,StartDate) between h.StartDate and h.EndDate

Post #984254
Posted Saturday, September 11, 2010 12:26 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 7, 2012 2:26 AM
Points: 24, Visits: 93
Here it is :)

--===== If test table exists, drop it
IF OBJECT_ID('TempDB..PublicHols','U') IS NOT NULL
DROP TABLE PublicHols

--===== Create test table
CREATE TABLE PublicHols
(
ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
Descr CHAR(64),
StartDate DATETIME,
EndDate DATETIME
)

--===== Special conditions
SET DATEFORMAT DMY

--===== Insert test data into test table
INSERT INTO PublicHols (Descr,StartDate,EndDate)
SELECT 'Fiestas Patrias','17/09/2007', '19/09/2007'
UNION ALL
SELECT 'Thanksgiving Break','22/11/2007', '23/11/2007'
UNION ALL
SELECT 'Australia Day','26/01/2010', '26/01/2010'
UNION ALL
SELECT 'Anzac Day','25/04/2010', '25/04/2010'
UNION ALL
SELECT 'Christmas Break','25/12/2010', '26/12/2010'

--==== Gather the data
select
h.ID,
h.Descr,
h.StartDate,
h.EndDate,
cast(h.EndDate-h.StartDate as integer)+1 as Days
from PublicHols h

--==== One solution to the problem (from another forum)
select
h.ID,
h.Descr,
dateadd(dd, n.number, h.StartDate) as HolDate,
1 as Days
from master..spt_values n
join PublicHols h on n.type = 'P'
and dateadd(dd,n.number,h.StartDate) between h.StartDate and h.EndDate

Post #984255
Posted Saturday, September 11, 2010 1:53 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, May 10, 2011 2:08 PM
Points: 405, Visits: 2,670
A variation using CROSS APPLY

;WITH cteTally (N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0))
FROM master..syscolumns
)
SELECT P.ID, P.Descr, Z.HolDate, 1 AS Days
FROM PublicHols AS P
CROSS APPLY
(
SELECT DATEADD(DAY, N, StartDate) - 1
FROM cteTally
WHERE N < DATEDIFF(DAY, StartDate, EndDate) + 2
) AS Z (HolDate)
ORDER BY HolDate


Post #984258
Posted Sunday, September 12, 2010 1:36 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 2:41 PM
Points: 5,367, Visits: 8,989
steve-893342 (9/11/2010)
A variation using CROSS APPLY


This is a SQL 7/2000 forum... the cross apply won't work unless posted in the wrong forum.


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #984427
Posted Sunday, September 12, 2010 1:46 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, May 10, 2011 2:08 PM
Points: 405, Visits: 2,670
WayneS (9/12/2010)
steve-893342 (9/11/2010)
A variation using CROSS APPLY


This is a SQL 7/2000 forum... the cross apply won't work unless posted in the wrong forum.

shell_l_d (9/10/2010)
Wondering if there's a way to do a select query without a cursor (although easy enough to do, curious if can be done without one) to do something like this... here's a small scale sample of what I'm trying to do with a select query...

SQL Server 2000 (& 2005)


Yeh, but it says &2005 in the original post, right?
Post #984429
Posted Sunday, September 12, 2010 4:20 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 2:41 PM
Points: 5,367, Visits: 8,989
steve-893342 (9/12/2010)
WayneS (9/12/2010)
steve-893342 (9/11/2010)
A variation using CROSS APPLY


This is a SQL 7/2000 forum... the cross apply won't work unless posted in the wrong forum.

shell_l_d (9/10/2010)
Wondering if there's a way to do a select query without a cursor (although easy enough to do, curious if can be done without one) to do something like this... here's a small scale sample of what I'm trying to do with a select query...

SQL Server 2000 (& 2005)


Yeh, but it says &2005 in the original post, right?


I'll grant you that. My interpretation is that it would need to run on both versions.


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #984456
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse