Author |
Topic |
uja100
Starting Member
6 Posts |
Posted - 2011-12-02 : 09:28:33
|
I have the following table:|Rules| |Reference Date| |Length| |CurrStart||Rule1| |04-12-1993| |14| |11-28-2011||Rule2| |01-02-2005| | 7| |12-01-2011||Rule3| |04-01-1996| | 7| |11-28-2011|What I'm looking to do is:For each rule, output (Reference Date + Length), until Reference Date = CurrStartFor example:Rule104-12-199304-26-1993...10-16-201110-30-201111-14-201111-28-2011Thanks in advance... |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-02 : 09:35:35
|
[code]DECLARE @rule varchar(100)SET @rule='Rule1';With CTE([Reference Date],Length,CurrStart)AS(SELECT [Reference Date],Length,CurrStartFROM TableWHERE Rules = @ruleUNION ALLSELECT DATEADD(dd,Length,[Reference Date]),Length,CurrStartFROM CTEWHERE DATEADD(dd,Length,[Reference Date])<= CurrStart)SELECT [Reference Date]FROM CTEOPTION (MAXRECURSION 0)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-02 : 09:38:53
|
maybe something likeoops - this is getting very similar to visakh16's now.;with cte as(select Rules, dte = ReferenceDate, length, enddte = CurrStart from tblunion allselect Rules, dte =dateadd(dd,length,dte), enddte = enddte from cte where dte<enddte)select Rules, dte from cteorder by Rules, dte==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
uja100
Starting Member
6 Posts |
Posted - 2011-12-02 : 11:11:32
|
quote: Originally posted by visakh16
DECLARE @rule varchar(100)SET @rule='Rule1';With CTE([Reference Date],Length,CurrStart)AS(SELECT [Reference Date],Length,CurrStartFROM TableWHERE Rules = @ruleUNION ALLSELECT DATEADD(dd,Length,[Reference Date]),Length,CurrStartFROM CTEWHERE DATEADD(dd,Length,[Reference Date])<= CurrStart)SELECT [Reference Date]FROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
uja100
Starting Member
6 Posts |
Posted - 2011-12-02 : 11:16:35
|
There was a problem with my previous reply.What I wanted to say is:Thank you visakh16, that seemed to work.I have two follwup questions:1. If i wanted just the last 20 results then where would I put the where clause and how would it look like.2. If I wanted to populate another table with the results then how could I do that. I was thinking Create Table D1 (Ref_Date)Insert into D1 (Ref_Date)<Put the CTE here>Thanks in advance... |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-02 : 11:21:47
|
with cte()insert tblSELECT top 20 [Reference Date]FROM CTEOPTION (MAXRECURSION 0)==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-02 : 12:11:50
|
quote: Originally posted by uja100 There was a problem with my previous reply.What I wanted to say is:Thank you visakh16, that seemed to work.I have two follwup questions:1. If i wanted just the last 20 results then where would I put the where clause and how would it look like.2. If I wanted to populate another table with the results then how could I do that. I was thinking Create Table D1 (Ref_Date)Insert into D1 (Ref_Date)<Put the CTE here>Thanks in advance...
it should beCreate Table D1 (Ref_Date)GODECLARE @rule varchar(100)SET @rule='Rule1';With CTE([Reference Date],Length,CurrStart)AS(SELECT [Reference Date],Length,CurrStartFROM TableWHERE Rules = @ruleUNION ALLSELECT DATEADD(dd,Length,[Reference Date]),Length,CurrStartFROM CTEWHERE DATEADD(dd,Length,[Reference Date])<= CurrStart)Insert into D1 (Ref_Date)SELECT TOP 20 [Reference Date]FROM CTEORDER BY [Reference Date] DESCOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
uja100
Starting Member
6 Posts |
Posted - 2011-12-02 : 12:42:39
|
This is weird because I get the following output:Ref_Date-----------------------2011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-28 00:00:00.0002011-11-14 00:00:00.0002011-11-14 00:00:00.0002011-11-14 00:00:00.0002011-11-14 00:00:00.0002011-11-14 00:00:00.0002011-11-14 00:00:00.0002011-11-14 00:00:00.000I cannot figure out why its doing this.**********************Create Table D4 (Ref_Date DateTime)GODECLARE @rule varchar(100)SET @rule='Bi-weekly In Day';With CTE([Ref_Date],PayPeriodLength,CurrPayPeriodStart)AS(SELECT [Ref_Date],PayPeriodLength,CurrPayPeriodStartFROM D1WHERE FixedRule = @ruleUNION ALLSELECT DATEADD(dd,PayPeriodLength,[Ref_Date]),PayPeriodLength,CurrPayPeriodStartFROM CTEWHERE DATEADD(dd,PayPeriodLength,[Ref_Date])<= CurrPayPeriodStart)Insert into D4 (Ref_Date)SELECT TOP 20 [Ref_Date]FROM CTEORDER BY [Ref_Date] DESCOPTION (MAXRECURSION 0)Select * From D4Drop Table D4 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-02 : 12:53:03
|
hmm..how did output change now?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
uja100
Starting Member
6 Posts |
Posted - 2011-12-02 : 15:34:02
|
I found a work around.I adjusted: SELECT TOP 20 [Ref_Date]To: SELECT Distinct TOP 20 [Ref_Date]Now I'm getting the results.Btw,When I ran the original query it did give me duplicate entries for the first date, 04-12-1993, but then it processed the subsequent dates. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-03 : 00:25:23
|
quote: Originally posted by uja100 I found a work around.I adjusted: SELECT TOP 20 [Ref_Date]To: SELECT Distinct TOP 20 [Ref_Date]Now I'm getting the results.Btw,When I ran the original query it did give me duplicate entries for the first date, 04-12-1993, but then it processed the subsequent dates.
Oh ok...so you had duplicates?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
uja100
Starting Member
6 Posts |
Posted - 2011-12-08 : 17:18:30
|
I think I know why I have the duplicates.If it's what I think it is doesn't relate to the SQL Statement.Thank you... |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-09 : 00:22:43
|
ok...np ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|