﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Solve Problems Using Recursive CTE / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 06:33:58 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>[quote][b]bj_shenglong (12/6/2012)[/b][hr][quote]I put the date into the query to make the Island query similar to the last query you posted.  That query will only report the first consecutive month for each name, however the Island query will report all consecutive months in the period for a name.  For example[/quote]That is what I said in previous post. This perticular task is to find when someone are qualified at his first time. So, recursion will search at beginning and stop searching after reaching the goal.[/quote]I've haven't looked at the queries in any great detail but I'm thinking that add TOP 1 would easily solve such a problem.</description><pubDate>Thu, 06 Dec 2012 13:04:07 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>[quote]I put the date into the query to make the Island query similar to the last query you posted.  That query will only report the first consecutive month for each name, however the Island query will report all consecutive months in the period for a name.  For example[/quote]That is what I said in previous post. This perticular task is to find when someone are qualified at his first time. So, recursion will search at beginning and stop searching after reaching the goal.</description><pubDate>Thu, 06 Dec 2012 12:28:10 GMT</pubDate><dc:creator>bj_shenglong</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>[quote][b]bj_shenglong[/b]I have one question for this test.It seems the test data starts at 1999-12-01, but queries use '2012-01-01' to start. I am not sure what the end date is.I was wondering if it makes any difference after we make some change on start date in these 2 queries, so that start date = the earlies test date, then sorting may go through all period for query 1. Of course, recursion will have more joins for query 2.[/quote]I put the date into the query to make the Island query similar to the last query you posted.  That query will only report the first consecutive month for each name, however the Island query will report all consecutive months in the period for a name.  For exampleA  2000-01-01  2 [b]A  2000-02-01  3A  2000-03-01  4  &amp;lt;-- Returned by RecursiveA  2000-04-01  3        &amp;lt;-- Returned By Island[/b]A  2000-05-01  1A  2000-06-01  2A  2000-07-01  1[b]A  2000-08-01  3A  2000-09-01  3A  2000-10-01  3A  2000-11-01  4        &amp;lt;-- Returned By Island [/b]A  2000-12-01  2What I found interesting was even though I was only adding data prior to those dates the recursive query kept getting more expensive.</description><pubDate>Thu, 06 Dec 2012 11:35:44 GMT</pubDate><dc:creator>mickyT</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>[quote][b]mickyT (12/5/2012)[/b][hr]So comparing the islands and recursive queries returning similar rows[code="sql"];WITH cte AS (	SELECT name, 		DATEADD(mm, - ROW_NUMBER() OVER (ORDER BY name, saledate), saledate) dategroup,		saledate	FROM #sales 	WHERE quantity &amp;gt; 2  and saledate &amp;gt;= '2012-01-01'	)SELECT name, max(saledate), COUNT(*)FROM cteGROUP BY name, dategroupHAVING COUNT(*) &amp;gt; 1 ORDER BY name, dategroup;with m2_cte_f (name,saledate,quantity,ind) as (select s.*, 0 as indfrom #sales swhere s.saledate='2012-01-01'union allselect s.*, case when s.quantity &amp;gt; 2 and sc.quantity &amp;gt; 2 then 1 else 0 end as indfrom #sales sinner join m2_cte_f sc on (s.saledate = dateadd(month,1,sc.saledate) and s.name=sc.name)where sc.ind = 0 )select * from m2_cte_f where ind=1 [/code]I get the following IO stats (timing not worth mentioning 1ms each) for the small test set[code="plain"](3 row(s) affected)Table '#sales____00000000009F'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.(3 row(s) affected)Table 'Worktable'. Scan count 2, logical reads 91, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table '#sales____00000000009F'. Scan count 2, logical reads 14, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.[/code]Upping the stakes a tiny bit by putting a moderate amount of data (3000 odd rows) into the table[code="sql"]INSERT INTO #sales (name, saledate)SELECT *FROM	(SELECT * FROM (VALUES('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T')) as sales(name)) names,	(SELECT TOP 156 dateadd(mm, N, '1999-12-01') saledate FROM Tally) as months UPDATE #salesSET quantity = RAND(Checksum(Newid())) * 5CREATE CLUSTERED INDEX SALES_IDX1 ON #sales (saledate, name)[/code]Thanks for all updates on this topic.I have one question for this test.It seems the test data starts at 1999-12-01, but queries use '2012-01-01' to start. I am not sure what the end date is.I was wondering if it makes any difference after we make some change on start date in these 2 queries, so that start date = the earlies test date, then sorting may go through all period for query 1. Of course, recursion will have more joins for query 2.</description><pubDate>Thu, 06 Dec 2012 10:08:30 GMT</pubDate><dc:creator>bj_shenglong</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>this is my contribution[code]USE tempdbGOIF OBJECT_ID('TestTbl') IS NOT NULL	DROP TABLE TestTblCREATE TABLE Testtbl (id INT PRIMARY KEY) INSERT INTO Testtbl(	id)SELECT TOP 1000 ROW_NUMBER() OVER ( ORDER BY c.object_id) id FROM sys.[columns] c ,sys.[columns] c2DELETE FROM TestTbl WHERE id IN(SELECT top 100 ABS(CHECKSUM(NEWID())%1000) FROM sys.[columns] c)DELETE FROM TestTbl WHERE id IN(SELECT top 100 ABS(CHECKSUM(NEWID())%1000) FROM sys.[columns] c)SELECT * FROM TestTbl;WITH S AS (SELECT ROW_NUMBER() OVER (order by t.id) AS RN,t.id FROM  TestTbl t LEFT OUTER JOIN TestTbl t2 ON t.id -1= t2.id WHERE t2.id IS NULL ),E AS (SELECT ROW_NUMBER() OVER (order by t.id) AS RN, t.id FROM TestTbl t LEFT OUTER JOIN TestTbl t2 ON t.id +1= t2.idWHERE t2.id IS NULL )SELECT s.id AS [START], e.id AS [END] FROM S INNER JOIN E ON s.Rn= E.rn GO[/code]using this i try to solve your problem[code]USE tempdbGOIF OBJECT_ID('sales') IS NOT NULL	DROP TABLE salesCREATE TABLE sales([name] [varchar](50) NOT NULL,[saledate] [datetime] NULL,[quantity] [int] NULL)GOInsert into sales(name,saledate,quantity)values('A','2012-01-01',1),('A','2012-02-01',2),('A','2012-03-01',3),('A','2012-04-01',4),('A','2012-05-01',5),('A','2012-06-01',6),('B','2012-01-01',6),('B','2012-02-01',2),('B','2012-03-01',3),('B','2012-04-01',4),('B','2012-05-01',1),('B','2012-06-01',6),('C','2012-01-01',6),('C','2012-02-01',1),('C','2012-03-01',3),('C','2012-04-01',1),('C','2012-05-01',4),('C','2012-06-01',1),('D','2012-01-01',6),('D','2012-02-01',3),('D','2012-03-01',3),('D','2012-04-01',4),('D','2012-05-01',1),('D','2012-06-01',6);WITH Fil AS (                SELECT *                FROM   sales                WHERE  quantity &amp;gt; 2            ), 	 S AS (         SELECT ROW_NUMBER() OVER (ORDER BY t.name) AS RN, t.NAME, MONTH (t.saledate) AS id         FROM   Fil t                LEFT OUTER JOIN Fil t2                     ON  MONTH (t.saledate) -1 = MONTH (t2.saledate) AND                         t2.name = t.name         WHERE  t2.saledate IS NULL     ),E AS (         SELECT ROW_NUMBER() OVER (ORDER BY t.name) AS RN, t.NAME, MONTH (t.saledate) AS id         FROM   Fil t                LEFT OUTER JOIN Fil t2                     ON  MONTH (t.saledate) + 1 = MONTH (t2.saledate) AND                         t2.name = t.name         WHERE  t2.saledate IS NULL     ),Gap AS(          SELECT e.NAME, s.id AS [DateStart], e.id AS [DateEnd]          FROM   S                 INNER JOIN E                      ON  s.Rn = E.rn                       --AND s.id&amp;lt;&amp;gt;e.id                  )SELECT g.NAME ,g.Datestart, sum(CASE WHEN(sales.quantity&amp;gt;2) AND MONTH(sales.saledate) BETWEEN g.datestart AND g.dateend THEN 1 ELSE 0 END ) as ResFROM sales INNER JOIN gap g ON sales.name =g.NAME  AND month(sales.saledate)&amp;gt;= g.datestart GROUP BY g.name,g.DatestartORDER BY g.name,g.Datestart[/code]i miss the criteria</description><pubDate>Wed, 05 Dec 2012 22:32:38 GMT</pubDate><dc:creator>thava</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>[quote][b]bj_shenglong (12/5/2012)[/b][hr]There is a particular problem, let's' say, we just want to find out who sold more then 2 each month in at least two consecutive months and when.For the above sample, below recursion would work. Also, recursion will stop immediately when it reaches the first qualified date.[/quote]Although they can be fast, recursive CTEs are still procedural in nature.  The only way to know for sure is to do a test.{Edit} Was distracted by a code promotion going on at work and I see that MickyT made just such a test.  Thank you, good Sir!</description><pubDate>Wed, 05 Dec 2012 21:22:23 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>So comparing the islands and recursive queries returning similar rows[code="sql"];WITH cte AS (	SELECT name, 		DATEADD(mm, - ROW_NUMBER() OVER (ORDER BY name, saledate), saledate) dategroup,		saledate	FROM #sales 	WHERE quantity &amp;gt; 2  and saledate &amp;gt;= '2012-01-01'	)SELECT name, max(saledate), COUNT(*)FROM cteGROUP BY name, dategroupHAVING COUNT(*) &amp;gt; 1 ORDER BY name, dategroup;with m2_cte_f (name,saledate,quantity,ind) as (select s.*, 0 as indfrom #sales swhere s.saledate='2012-01-01'union allselect s.*, case when s.quantity &amp;gt; 2 and sc.quantity &amp;gt; 2 then 1 else 0 end as indfrom #sales sinner join m2_cte_f sc on (s.saledate = dateadd(month,1,sc.saledate) and s.name=sc.name)where sc.ind = 0 )select * from m2_cte_f where ind=1 [/code]I get the following IO stats (timing not worth mentioning 1ms each) for the small test set[code="plain"](3 row(s) affected)Table '#sales____00000000009F'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.(3 row(s) affected)Table 'Worktable'. Scan count 2, logical reads 91, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table '#sales____00000000009F'. Scan count 2, logical reads 14, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.[/code]Upping the stakes a tiny bit by putting a moderate amount of data (3000 odd rows) into the table[code="sql"]INSERT INTO #sales (name, saledate)SELECT *FROM	(SELECT * FROM (VALUES('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T')) as sales(name)) names,	(SELECT TOP 156 dateadd(mm, N, '1999-12-01') saledate FROM Tally) as months UPDATE #salesSET quantity = RAND(Checksum(Newid())) * 5CREATE CLUSTERED INDEX SALES_IDX1 ON #sales (saledate, name)[/code]I get the following[code="plain"](22 row(s) affected)Table '#sales__0000000000A4'. Scan count 1, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times:   CPU time = 0 ms,  elapsed time = 1 ms.(15 row(s) affected)Table 'Worktable'. Scan count 2, logical reads 635, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table '#sales__0000000000A4'. Scan count 96, logical reads 193, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times:   CPU time = 0 ms,  elapsed time = 3 ms.[/code]As I added more rows to the table the recursive query got very gradually slower and did more reads, while the islands query remained static.  I got up to  75816 rows.   Would you believe the had sales data back to 1770 for the same 26 people :-D[code="plain"](27 row(s) affected)Table '#sales__0000000000AD'. Scan count 1, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times:   CPU time = 0 ms,  elapsed time = 1 ms.(19 row(s) affected)Table 'Worktable'. Scan count 2, logical reads 1193, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table '#sales__0000000000AD'. Scan count 189, logical reads 380, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times:   CPU time = 0 ms,  elapsed time = 5 ms.[/code]</description><pubDate>Wed, 05 Dec 2012 20:13:53 GMT</pubDate><dc:creator>mickyT</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>There is a particular problem, let's' say, we just want to find out who sold more then 2 each month in at least two consecutive months and when.For the above sample, below recursion would work. Also, recursion will stop immediately when it reaches the first qualified date.	with m2_cte_f (name,saledate,quantity,ind) as (	select s.*, 0 as ind	from #sales s	where s.saledate='2012-01-01'	union all	select s.*, case when s.quantity &amp;gt; 2 and sc.quantity &amp;gt; 2 then 1 else 0 end as ind	from #sales s	inner join m2_cte_f sc 	on (s.saledate = dateadd(month,1,sc.saledate) and s.name=sc.name)	where sc.ind = 0 	)	select * from m2_cte_f where ind=1</description><pubDate>Wed, 05 Dec 2012 17:41:00 GMT</pubDate><dc:creator>bj_shenglong</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>[quote][b]bj_shenglong (12/5/2012)[/b][hr]But I assume this way could be faster...[/quote]That's how rumors of performance get started. :-)Write code to build a million row test table and test your hypothesis.  No matter which way it turns out, we'll all learn something if you post the results.  See the following articles for how to do such a thing pretty easily and quickly.[url]http://www.sqlservercentral.com/articles/Data+Generation/87901/[/url][url]http://www.sqlservercentral.com/articles/Test+Data/88964/[/url]</description><pubDate>Wed, 05 Dec 2012 16:39:32 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>I know Jeff Moden's solution.But I assume this way could be faster because there is not sorting for row number.  At least, it is a different solution.</description><pubDate>Wed, 05 Dec 2012 16:04:30 GMT</pubDate><dc:creator>bj_shenglong</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>Sales department requires to find out number of months a product has been sold more then certain amount in consecutive month.If data is just in few months or just require for 2 or 3 consecutive months, then simple table join can do it.But we have like years rows, and need count all consecutive months, it will could be hard to hard coding all table join for 2, 3, 4 , 5 ... consecutive months.There is an easy way using recursive CTE to solve such a problem.For example, a table store Sales rows like below. And we need to list number of consecutive months for any salesman who sold a product more then 2 in a month. It is can be done easily by following recursive CTE queryCREATE TABLE Sales(salesman_name CHAR(10) NOT NULL,  sale_date CHAR(10) NOT NULL,   PRIMARY KEY (salesman_name, sale_date), sale_qty INTEGER NOT NULL  CHECK (sale_qty &amp;gt; 0));INSERT INTO SalesVALUES('A', '2012-01-00', 1), ('A', '2012-02-00', 2), ('A', '2012-03-00', 3), ('A', '2012-04-00', 4), ('A', '2012-05-00', 5), ('A', '2012-06-00', 6),('B', '2012-01-00', 6), ('B', '2012-02-00', 2), ('B', '2012-03-00', 3), ('B', '2012-04-00', 4), ('B', '2012-05-00', 1), ('B', '2012-06-00', 6), ('C', '2012-01-00', 6), ('C', '2012-02-00', 1), ('C', '2012-03-00', 3), ('C', '2012-04-00', 1), ('C', '2012-05-00', 4), ('C', '2012-06-00', 1), ('D', '2012-01-00', 6), ('D', '2012-02-00', 3), ('D', '2012-03-00', 3), ('D', '2012-04-00', 4), ('D', '2012-05-00', 1), ('D', '2012-06-00', 6);WITH X1 AS (SELECT salesman_name, sale_date, sale_qty,        ROW_NUMBER() OVER (PARTITION BY salesman_name 		               ORDER BY salesman_name, sale_date) AS r1  FROM Sales),  X2  AS   (SELECT salesman_name, sale_date, sale_qty,           r1-          ROW_NUMBER() OVER (PARTITION BY salesman_name 		               ORDER BY salesman_name, sale_date) 	     AS sale_grp    FROM X1   WHERE sale_qty &amp;gt; 2)   SELECT salesman_name, MIN(sale_date), MAX(sale_date)     FROM X2  GROUP BY salesman_name, sale_grp;</description><pubDate>Wed, 05 Dec 2012 15:37:27 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>Using Jeff Moden's article [url]http://www.sqlservercentral.com/articles/T-SQL/71550/[/url] about Group Islands of Contiguous Dates as inspiration, you could do the following[code="sql"];WITH cte AS (	SELECT name, 		DATEADD(mm, - ROW_NUMBER() OVER (ORDER BY name, saledate), saledate) dategroup,		saledate	FROM #sales 	WHERE  quantity &amp;gt; 2	)SELECT name, MIN(saledate) firstsaledate, COUNT(*) cntFROM cteGROUP BY name, dategroupORDER BY name, dategroup[/code]</description><pubDate>Wed, 05 Dec 2012 15:08:40 GMT</pubDate><dc:creator>mickyT</dc:creator></item><item><title>RE: Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>This looks like a gaps and island problem.  it also from the problem description sounds like homework or an interview question.Take a look at this for a detailed explanation and solution of the problem.  [url]http://www.manning.com/nielsen/nielsenMEAP_freeCh5.pdf[/url]  Specifically part two chapter 5</description><pubDate>Wed, 05 Dec 2012 15:04:42 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>Solve Problems Using Recursive CTE</title><link>http://www.sqlservercentral.com/Forums/Topic1393173-392-1.aspx</link><description>Problem: Bank requires to find out number of  months customs have spent money more then certain amount in consecutive monthor sales department requires to find out number of months a product has been sold more then certain amount in consecutive month.If data is just in few months or just require for 2 or 3 consecutive months, then simple table join can do it.But we have like years records, and need count all consecutive months, it will could be hard to hard coding all table join for 2, 3, 4 ,5 ... consecutive months.There is an easy way using recursive CTE to solve such a problem.For example, a table store sales records like below. And we need to list number of consecutive months for any sale who sold a product more then 2 in a month. It is can be done easily by following recursive CTE querysample dataCREATE TABLE #sales(	[name] [varchar](50) NOT NULL,	[saledate] [datetime] NULL,	[quantity] [int] NULL)	insert into #sales(name,saledate,quantity)values	('A','2012-01-01',1),	('A','2012-02-01',2),	('A','2012-03-01',3),	('A','2012-04-01',4),	('A','2012-05-01',5),	('A','2012-06-01',6)	insert into #sales(name,saledate,quantity)values	('B','2012-01-01',6),	('B','2012-02-01',2),	('B','2012-03-01',3),	('B','2012-04-01',4),	('B','2012-05-01',1),	('B','2012-06-01',6)	insert into #sales(name,saledate,quantity)values	('C','2012-01-01',6),	('C','2012-02-01',1),	('C','2012-03-01',3),	('C','2012-04-01',1),	('C','2012-05-01',4),	('C','2012-06-01',1)	insert into #sales(name,saledate,quantity)values	('D','2012-01-01',6),	('D','2012-02-01',3),	('D','2012-03-01',3),	('D','2012-04-01',4),	('D','2012-05-01',1),	('D','2012-06-01',6)-- wm for all months in which product sold more 2with wm as (    select name,saledate    from #sales     where quantity&amp;gt;2    ),-- only using above qualified records, not all records to do recursive join    base_cte (name,saledate ) as (    select * from wm    union all    select a.name,a.saledate    from wm a inner join base_cte b    on a.name=b.name and a.saledate=dateadd(month,1,b.saledate)    )-- the count column indicates number of consecutive month for that month.    select b.name,b.saledate, COUNT(b.name) as cnt	from base_cte b	group by b.name,b.saledate	order by b.name,b.saledate-- for example cnt = 2, meaning that month is the second month, from which, backward in 2 -- consecutive month, a product was sold more then 2 each month --                   cnt = 3, meaning that month is the third month,  from which, backward in 3 -- consecutive month, a product was sold more then 2</description><pubDate>Wed, 05 Dec 2012 12:41:42 GMT</pubDate><dc:creator>bj_shenglong</dc:creator></item></channel></rss>