SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server Function: Cursor or Tally Table; which to use, HELP


SQL Server Function: Cursor or Tally Table; which to use, HELP

Author
Message
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29559 Visits: 39986
can you convert that into a CTE or insert statemetns, the way i did?

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13253 Visits: 8566
LOVE the pic Lowell!! Your code rocks too, btw.

Oh, and I would like to request that the OP makes whomever designed that system to drop and give me 20!!! w00t

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
Knives85
Knives85
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 257
Like this ?


WITH CTE(reference,StartDate,Pattern,PatternLength) AS
(
SELECT 'TestRef-AA',convert(datetime,'01/01/2013 10:45'),' 1111 111 11','15'
SELECT 'TestRef-AA',convert(datetime,'10/11/2013 09:00'),'11 11 111 ','12'
SELECT 'TestRef-AA',convert(datetime,'09/09/2013 13:00'),'11111 11 ','9'
SELECT 'TestRef-AA',convert(datetime,'02/08/2013 12:00'),'1 11 1 ','11'
)



:-)
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29559 Visits: 39986
good start Knives;
so here's my same query, with your updated data in it;

so if you run this and look at the data, does it produce the desired results?

code]
;WITH
TENS (N) AS (SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0),
THOUSANDS (N) AS (SELECT 1 FROM TENS t1 CROSS JOIN TENS t2 CROSS JOIN TENS t3),
TALLY (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM THOUSANDS)
,
myCTE(reference,StartDate,Pattern,PatternLength) AS
(
SELECT 'TestRef-AA',convert(datetime,'01/01/2013 10:45'),' 1111 111 11','15' UNION ALL
SELECT 'TestRef-BB',convert(datetime,'10/11/2013 09:00'),'11 11 111 ','12' UNION ALL
SELECT 'TestRef-CC',convert(datetime,'09/09/2013 13:00'),'11111 11 ','9' UNION ALL
SELECT 'TestRef-DD',convert(datetime,'02/08/2013 12:00'),'1 11 1 ','11'
)
SELECT CASE WHEN SUBSTRING([pattern],N,1) = '1'
THEN DATEADD(wk,Tally.N,[startdate])
ELSE NULL
END As CalcDate,*,
SUBSTRING([pattern],N,1)
FROM MyCTE
CROSS JOIN Tally
WHERE N <= LEN([pattern])
--AND SUBSTRING([pattern],N,1) = '1'
order by reference, N

[/code]

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Knives85
Knives85
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 257
Thanks Lowell, I'll have a look first thing.The more I think about it, the more I think it'll work. Thanks, your a legend :-)
Knives85
Knives85
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 257
Hi lowell

I've tested the code and it worked beautifully. I need a little help though, I got the logic wrong when I explained the issue originally. There should never be any instance of the pattern not starting with a 1;however, the very first row generated needs to be the very first instance of the start date, not the start date with 7 adds added. I have tried to get around this by slightly altering the case statement to replace the first row date with the actual start date but then the second row date that is generated doesn't use the replace start date from the first row but the original, meaning that it's out by a week.

Any ideas?

Here's the code I've changed


;WITH

TENS (N) AS (SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL

SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL

SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0),

THOUSANDS (N) AS (SELECT 1 FROM TENS t1 CROSS JOIN TENS t2 CROSS JOIN TENS t3),

TALLY (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM THOUSANDS)

,

TestCTE(reference,StartDate,Pattern,PatternLength) AS

(

SELECT 'VideoGames-S1',convert(datetime,'01/01/2013 10:45'),'11 111 111111  1111','18' --Course 13NAPUSD-B2

)

 

SELECT

                                'StartTime' =

                                CASE

                                WHEN Tally.N = 1 THEN [startdate]

                                WHEN SUBSTRING([pattern],N,1) = '1'  THEN DATEADD(wk,Tally.N,[startdate]) ELSE NULL 

                                END,

                                TestStartDate_DATEADD = DATEADD(wk,Tally.N,[startdate])

                                ,*

                                , SUBSTRING([pattern],N,1)

                               

FROM

                                TestCTE testcte

CROSS JOIN Tally tally

 

WHERE

                                N <= LEN([pattern])

--AND SUBSTRING([pattern],N,1) = '1'

 

ORDER BY

                                reference, N



Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29559 Visits: 39986
knives the fix is rediculously easy;
the Tally table needs to start at zero instead of one.

so this tiny subsection changes:

TALLY (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM THOUSANDS)
--change to
TALLY (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) - 1 FROM THOUSANDS)




Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Knives85
Knives85
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 257
Hi lowell

I've managed to sort it now, figured it out about 2 mins after posting. Having a blond day today.

In the THEN clause I just -7 after the calculation. This moves all predicted dates back 7 days.

Sorry for not updating post, only just had a spare minute.

Thanks for the help, it's appreciated :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search