<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL Server &#8211; Multiple CTE in One SELECT Statement Query</title>
	<atom:link href="http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Wed, 22 May 2013 19:03:49 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: krishna</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-476968</link>
		<dc:creator><![CDATA[krishna]]></dc:creator>
		<pubDate>Fri, 17 May 2013 07:22:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-476968</guid>
		<description><![CDATA[hi sir,
   please help me my problem, single field name in multiple comma separated value to use like qury and get value how write the query? 
sample column..

Software testing,maual testing,automation testing
PHP,Multimedia
PHP,ASP.NET,NET,Html
testing,Javascript,Html
Asp.net,Sql Server,Software testing
Asp.net,PHP]]></description>
		<content:encoded><![CDATA[<p>hi sir,<br />
   please help me my problem, single field name in multiple comma separated value to use like qury and get value how write the query?<br />
sample column..</p>
<p>Software testing,maual testing,automation testing<br />
PHP,Multimedia<br />
PHP,ASP.NET,NET,Html<br />
testing,Javascript,Html<br />
Asp.net,Sql Server,Software testing<br />
Asp.net,PHP</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: david</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-457844</link>
		<dc:creator><![CDATA[david]]></dc:creator>
		<pubDate>Wed, 17 Apr 2013 08:55:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-457844</guid>
		<description><![CDATA[GREAT STUFF! The comma between the two cte definitions was the missing link for me... 
Thanks for the effort]]></description>
		<content:encoded><![CDATA[<p>GREAT STUFF! The comma between the two cte definitions was the missing link for me&#8230;<br />
Thanks for the effort</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sridhar</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-429129</link>
		<dc:creator><![CDATA[Sridhar]]></dc:creator>
		<pubDate>Thu, 28 Feb 2013 11:28:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-429129</guid>
		<description><![CDATA[Hi Mandar Kavishwar,
Here is your solution without cursor, using CTE,

WITH
ProductMaster(Product_ID, ProductName, Price)
AS
(
SELECT 1, &#039;XYZ&#039;, 20.10 UNION ALL
SELECT 2, &#039;ABC&#039;, 11.35 UNION ALL
SELECT 3, &#039;PQR&#039;, 05.33
),
Genre(Genre_ID, GenreName)
AS
(
SELECT 101, &#039;Horror&#039; UNION ALL
SELECT 102, &#039;Romantic&#039; UNION ALL
SELECT 103, &#039;Suspense&#039;
),
Product_Genre(Genre_ID, ProductID)
AS
(
SELECT 101, 1 UNION ALL
SELECT 102, 1 UNION ALL
SELECT 101, 2 UNION ALL
SELECT 101, 3 UNION ALL
SELECT 103, 3
),

cte3 as(
SELECT
row_number() over(partition by ProductMaster.Product_ID order by ProductMaster.Product_ID) num,
Product_ID,
ProductName,
Genre.GenreName,
Price
FROM
ProductMaster,
Genre,
Product_Genre
WHERE
Product_Genre.Genre_ID = Genre.Genre_ID
AND Product_Genre.ProductID = ProductMaster.Product_ID -- )test
),

cte4 as
(
select * from cte3 where num=1
),

cte5 as
(
select num,product_id,null as ProductName,GenreName,null as price from cte3 where num1
)

select cte4.Product_ID,
cte4.ProductName,
cast(cte4.GenreName as varchar(100)) + (case when cte5.GenreName is null then &#039;&#039; else &#039; &#124; &#039; end) 
+ (case when cte5.GenreName is null then &#039;&#039; else cte5.GenreName end) as GenreName,
cte4.Price 
from cte4 left outer join cte5 on cte4.Product_ID = cte5.Product_ID]]></description>
		<content:encoded><![CDATA[<p>Hi Mandar Kavishwar,<br />
Here is your solution without cursor, using CTE,</p>
<p>WITH<br />
ProductMaster(Product_ID, ProductName, Price)<br />
AS<br />
(<br />
SELECT 1, &#8216;XYZ&#8217;, 20.10 UNION ALL<br />
SELECT 2, &#8216;ABC&#8217;, 11.35 UNION ALL<br />
SELECT 3, &#8216;PQR&#8217;, 05.33<br />
),<br />
Genre(Genre_ID, GenreName)<br />
AS<br />
(<br />
SELECT 101, &#8216;Horror&#8217; UNION ALL<br />
SELECT 102, &#8216;Romantic&#8217; UNION ALL<br />
SELECT 103, &#8216;Suspense&#8217;<br />
),<br />
Product_Genre(Genre_ID, ProductID)<br />
AS<br />
(<br />
SELECT 101, 1 UNION ALL<br />
SELECT 102, 1 UNION ALL<br />
SELECT 101, 2 UNION ALL<br />
SELECT 101, 3 UNION ALL<br />
SELECT 103, 3<br />
),</p>
<p>cte3 as(<br />
SELECT<br />
row_number() over(partition by ProductMaster.Product_ID order by ProductMaster.Product_ID) num,<br />
Product_ID,<br />
ProductName,<br />
Genre.GenreName,<br />
Price<br />
FROM<br />
ProductMaster,<br />
Genre,<br />
Product_Genre<br />
WHERE<br />
Product_Genre.Genre_ID = Genre.Genre_ID<br />
AND Product_Genre.ProductID = ProductMaster.Product_ID &#8212; )test<br />
),</p>
<p>cte4 as<br />
(<br />
select * from cte3 where num=1<br />
),</p>
<p>cte5 as<br />
(<br />
select num,product_id,null as ProductName,GenreName,null as price from cte3 where num1<br />
)</p>
<p>select cte4.Product_ID,<br />
cte4.ProductName,<br />
cast(cte4.GenreName as varchar(100)) + (case when cte5.GenreName is null then &#8221; else &#8216; | &#8216; end)<br />
+ (case when cte5.GenreName is null then &#8221; else cte5.GenreName end) as GenreName,<br />
cte4.Price<br />
from cte4 left outer join cte5 on cte4.Product_ID = cte5.Product_ID</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abbi Samuels</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-422316</link>
		<dc:creator><![CDATA[Abbi Samuels]]></dc:creator>
		<pubDate>Sat, 16 Feb 2013 01:34:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-422316</guid>
		<description><![CDATA[Need help on a CTE...

We have a recursive chaining that was done and I need to get the first part of the chain for all rows in the table.

Another words...

Table Widget
PK_id     old_id     new_id
1              1               2
2              2               3
3              3               4
4            15             16 
5            16             17
6            17             18
7            18             19
8          100           105
9          105           110
10        110             91
11	    777	     111
12	    111	     401
13	     401	     845
There are 4 chains there and I need to bring back the first of the chain so I would get:
1
15
100
777


I have a query that can bring back the entire chain but can&#039;t figure out how to get the beginning of the chain for each recursive chain.  The query below is not exactly correct as you may notice.  It brings back one extra row which is not correct.  

With CTE (new_id, old_id, rn, level)
as 
	(select old_id, new_id, ROW_NUMBER() OVER ( ORDER BY PK_id) AS RN, 0 as level
	from xref_system LatestSystem where old_id =&#039;777&#039;
	union all
	select c.new_id, c.old_id, pc.RN, Level + 1
	from widget as c
	inner join cte as pc on c.old_id = pc.new_id
	)
select new_id, old_id, RN, level 
from CTE
ORDER BY rn, LEVEL

This brings back:
new_id	old_id	RN	level
777	        111	        1	0
111	        777	        1	1
401	        111	        1	2
845	        401	        1	3

So, there are 2 questions....  
1)  The above query is not correct.
2)   Would like a query to go through an entire table and bring back the following result:
1
15
100
77

Thanks in advance!
Abbi]]></description>
		<content:encoded><![CDATA[<p>Need help on a CTE&#8230;</p>
<p>We have a recursive chaining that was done and I need to get the first part of the chain for all rows in the table.</p>
<p>Another words&#8230;</p>
<p>Table Widget<br />
PK_id     old_id     new_id<br />
1              1               2<br />
2              2               3<br />
3              3               4<br />
4            15             16<br />
5            16             17<br />
6            17             18<br />
7            18             19<br />
8          100           105<br />
9          105           110<br />
10        110             91<br />
11	    777	     111<br />
12	    111	     401<br />
13	     401	     845<br />
There are 4 chains there and I need to bring back the first of the chain so I would get:<br />
1<br />
15<br />
100<br />
777</p>
<p>I have a query that can bring back the entire chain but can&#8217;t figure out how to get the beginning of the chain for each recursive chain.  The query below is not exactly correct as you may notice.  It brings back one extra row which is not correct.  </p>
<p>With CTE (new_id, old_id, rn, level)<br />
as<br />
	(select old_id, new_id, ROW_NUMBER() OVER ( ORDER BY PK_id) AS RN, 0 as level<br />
	from xref_system LatestSystem where old_id =&#8217;777&#8242;<br />
	union all<br />
	select c.new_id, c.old_id, pc.RN, Level + 1<br />
	from widget as c<br />
	inner join cte as pc on c.old_id = pc.new_id<br />
	)<br />
select new_id, old_id, RN, level<br />
from CTE<br />
ORDER BY rn, LEVEL</p>
<p>This brings back:<br />
new_id	old_id	RN	level<br />
777	        111	        1	0<br />
111	        777	        1	1<br />
401	        111	        1	2<br />
845	        401	        1	3</p>
<p>So, there are 2 questions&#8230;.<br />
1)  The above query is not correct.<br />
2)   Would like a query to go through an entire table and bring back the following result:<br />
1<br />
15<br />
100<br />
77</p>
<p>Thanks in advance!<br />
Abbi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joey Dubuc</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-418776</link>
		<dc:creator><![CDATA[Joey Dubuc]]></dc:creator>
		<pubDate>Wed, 06 Feb 2013 22:42:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-418776</guid>
		<description><![CDATA[Thanks, Pinal Dave!]]></description>
		<content:encoded><![CDATA[<p>Thanks, Pinal Dave!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cynthia</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-418685</link>
		<dc:creator><![CDATA[Cynthia]]></dc:creator>
		<pubDate>Wed, 06 Feb 2013 17:59:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-418685</guid>
		<description><![CDATA[Im trying to insert the results into a table but I keep receiving error: Invalid object name &#039;perms&#039;. If within my WITH statement I only use a single select statement, it works.  Any suggestions?

IF OBJECT_ID (N&#039;dbo.tempdbperms&#039;, N&#039;U&#039;) IS NOT NULL
    DROP TABLE dbo.tempdbperms;
GO
CREATE TABLE dbo.tempdbperms
(
	[ServerName] [nvarchar](100) NULL,
	[dbname] [nvarchar](100) NULL,
	[principal_name] [nvarchar](50) NULL,
	[principal_id] [nvarchar](50) NULL,
	[principal_type_desc] [nvarchar](100) NULL,
	[class_desc] [nvarchar](50) NULL,
	[object_name] [nvarchar](100) NULL,
	[permission_name] [nvarchar](50) NULL,
	[permission_state_desc] [nvarchar](50) NULL
);
GO
DECLARE @DBNAME nvarchar (1000) = (SELECT DB_NAME()); 
DECLARE @ServerName nvarchar (1000) = (select @@servername);
WITH    perms (	[ServerName],
	[dbname],
	[principal_name],
	[principal_id],
	[principal_type_desc],
	[class_desc],
	[object_name],
	[permission_name],
	[permission_state_desc])

AS 
(
select  @ServerName as ServerName, 
		@DBNAME as dbname,
		USER_NAME(p.grantee_principal_id) AS principal_name,
		dp.principal_id,
		dp.type_desc AS principal_type_desc,
		p.class_desc,
		OBJECT_NAME(p.major_id) AS object_name,
		p.permission_name,
		p.state_desc AS permission_state_desc 
	from    sys.database_permissions p
	inner   JOIN sys.database_principals dp
	    on     p.grantee_principal_id = dp.principal_id
)

--users

SELECT @ServerName as ServerName, 
		@DBNAME as dbname, 
		p.principal_name,  
		p.principal_type_desc, 
		p.class_desc, p.[object_name],
		p.permission_name, 
		p.permission_state_desc, 
		cast(NULL as sysname) as role_name
FROM    perms p
WHERE   principal_type_desc  &#039;DATABASE_ROLE&#039;

UNION

--role members

SELECT @ServerName as ServerName, 
		@DBNAME as dbname, 
		rm.member_principal_name, 
		rm.principal_type_desc, 
		p.class_desc, 
		p.object_name, 
		p.permission_name, 
		p.permission_state_desc,
		rm.role_name
FROM    perms p
right outer JOIN 
(
	select @ServerName as ServerName, 
			@DBNAME as dbname, 
			role_principal_id, 
			dp.type_desc as principal_type_desc, 
			member_principal_id,
			user_name(member_principal_id) as member_principal_name,
			user_name(role_principal_id) as role_name--,*
	from    sys.database_role_members rm
    INNER   JOIN sys.database_principals dp
	ON     rm.member_principal_id = dp.principal_id

) rm

ON     rm.role_principal_id = p.principal_id
order by 1;
GO
INSERT INTO dbo.tempdbperms 
    SELECT *
    FROM perms;
GO]]></description>
		<content:encoded><![CDATA[<p>Im trying to insert the results into a table but I keep receiving error: Invalid object name &#8216;perms&#8217;. If within my WITH statement I only use a single select statement, it works.  Any suggestions?</p>
<p>IF OBJECT_ID (N&#8217;dbo.tempdbperms&#8217;, N&#8217;U') IS NOT NULL<br />
    DROP TABLE dbo.tempdbperms;<br />
GO<br />
CREATE TABLE dbo.tempdbperms<br />
(<br />
	[ServerName] [nvarchar](100) NULL,<br />
	[dbname] [nvarchar](100) NULL,<br />
	[principal_name] [nvarchar](50) NULL,<br />
	[principal_id] [nvarchar](50) NULL,<br />
	[principal_type_desc] [nvarchar](100) NULL,<br />
	[class_desc] [nvarchar](50) NULL,<br />
	[object_name] [nvarchar](100) NULL,<br />
	[permission_name] [nvarchar](50) NULL,<br />
	[permission_state_desc] [nvarchar](50) NULL<br />
);<br />
GO<br />
DECLARE @DBNAME nvarchar (1000) = (SELECT DB_NAME());<br />
DECLARE @ServerName nvarchar (1000) = (select @@servername);<br />
WITH    perms (	[ServerName],<br />
	[dbname],<br />
	[principal_name],<br />
	[principal_id],<br />
	[principal_type_desc],<br />
	[class_desc],<br />
	[object_name],<br />
	[permission_name],<br />
	[permission_state_desc])</p>
<p>AS<br />
(<br />
select  @ServerName as ServerName,<br />
		@DBNAME as dbname,<br />
		USER_NAME(p.grantee_principal_id) AS principal_name,<br />
		dp.principal_id,<br />
		dp.type_desc AS principal_type_desc,<br />
		p.class_desc,<br />
		OBJECT_NAME(p.major_id) AS object_name,<br />
		p.permission_name,<br />
		p.state_desc AS permission_state_desc<br />
	from    sys.database_permissions p<br />
	inner   JOIN sys.database_principals dp<br />
	    on     p.grantee_principal_id = dp.principal_id<br />
)</p>
<p>&#8211;users</p>
<p>SELECT @ServerName as ServerName,<br />
		@DBNAME as dbname,<br />
		p.principal_name,<br />
		p.principal_type_desc,<br />
		p.class_desc, p.[object_name],<br />
		p.permission_name,<br />
		p.permission_state_desc,<br />
		cast(NULL as sysname) as role_name<br />
FROM    perms p<br />
WHERE   principal_type_desc  &#8216;DATABASE_ROLE&#8217;</p>
<p>UNION</p>
<p>&#8211;role members</p>
<p>SELECT @ServerName as ServerName,<br />
		@DBNAME as dbname,<br />
		rm.member_principal_name,<br />
		rm.principal_type_desc,<br />
		p.class_desc,<br />
		p.object_name,<br />
		p.permission_name,<br />
		p.permission_state_desc,<br />
		rm.role_name<br />
FROM    perms p<br />
right outer JOIN<br />
(<br />
	select @ServerName as ServerName,<br />
			@DBNAME as dbname,<br />
			role_principal_id,<br />
			dp.type_desc as principal_type_desc,<br />
			member_principal_id,<br />
			user_name(member_principal_id) as member_principal_name,<br />
			user_name(role_principal_id) as role_name&#8211;,*<br />
	from    sys.database_role_members rm<br />
    INNER   JOIN sys.database_principals dp<br />
	ON     rm.member_principal_id = dp.principal_id</p>
<p>) rm</p>
<p>ON     rm.role_principal_id = p.principal_id<br />
order by 1;<br />
GO<br />
INSERT INTO dbo.tempdbperms<br />
    SELECT *<br />
    FROM perms;<br />
GO</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gayathri</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-403763</link>
		<dc:creator><![CDATA[gayathri]]></dc:creator>
		<pubDate>Fri, 04 Jan 2013 19:23:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-403763</guid>
		<description><![CDATA[Your post helped me solve multiple CTE issue.
Thanks for the article]]></description>
		<content:encoded><![CDATA[<p>Your post helped me solve multiple CTE issue.<br />
Thanks for the article</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jagdeep</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-356893</link>
		<dc:creator><![CDATA[jagdeep]]></dc:creator>
		<pubDate>Sun, 07 Oct 2012 16:00:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-356893</guid>
		<description><![CDATA[Hi Friends,

Can you suggest me different options to fire multiple select statement simultaneously from the same table except using sql script in the oracle and mysql]]></description>
		<content:encoded><![CDATA[<p>Hi Friends,</p>
<p>Can you suggest me different options to fire multiple select statement simultaneously from the same table except using sql script in the oracle and mysql</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-354014</link>
		<dc:creator><![CDATA[Tim]]></dc:creator>
		<pubDate>Fri, 28 Sep 2012 19:05:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-354014</guid>
		<description><![CDATA[The only reason I see using a with statement here is to avoid creating the tables.  I recommend a cursor for your request.

I&#039;ve changed your joins to outer in case you have a title without a Genre assigned or there is an orphaned entry in Product_Genre.

Also, I&#039;d change the &#039;union all&#039; to a simple union to reduce possible duplicity.  Now ABC is twice as horrific as you might think.  Just remove the single &#039;all&#039; and it will just be horror.

declare @priorID int, @priorName varchar(3), @priorPrice varchar(6), @priorGenreCurrent varchar(max);
declare @pID int, @pName varchar(3), @pPrice varchar(6), @pGenreCurrent varchar(20), @pGenreTxt varchar(max);
declare @pCounter int;

select @pCounter = 1;
select @priorID = 0;
select @priorName = null;
select @priorPrice = null;
select @priorGenreCurrent = null;
select @pGenreTxt = null;

declare myCursor cursor for 
--If you have tables comment out the with block
--Begin with block
WITH
 ProductMaster(Product_ID, ProductName, Price)
 AS
 (
 SELECT 1, &#039;XYZ&#039;, 20.10 UNION 
 SELECT 2, &#039;ABC&#039;, 11.35 UNION 
 SELECT 3, &#039;PQR&#039;, 05.33
 ),
 Genre(Genre_ID, GenreName)
 AS
 (
 SELECT 101, &#039;Horror&#039; UNION 
 SELECT 102, &#039;Romantic&#039; UNION 
 SELECT 103, &#039;Suspense&#039;
),
Product_Genre(Genre_ID, ProductID)
 AS
 (
 SELECT 101, 1 UNION 
 SELECT 102, 1 UNION 
 SELECT 101, 3 UNION 
 SELECT 103, 3 UNION
 SELECT 101, 2 UNION all
 SELECT 101, 2
 )
--End with block
SELECT
 ProductMaster.Product_ID,
 ProductMaster.ProductName,
 Genre.GenreName,
 ProductMaster.Price
FROM
 ProductMaster
 left outer join Product_Genre on ProductMaster.Product_ID = Product_Genre.ProductID 
 left outer join Genre on Product_Genre.Genre_ID = Genre.Genre_ID
 order by ProductMaster.Product_ID, Product_Genre.Genre_ID;
 
 open myCursor 
 fetch next from myCursor into @pID, @pName, @pGenreCurrent, @pPrice;
 while @@FETCH_STATUS = 0
 begin
	if (@priorID = 0) or (@priorID  @pID)
	begin
		print @priorName + &#039; &#039; +  @pGenreTxt + &#039; &#039; + @priorPrice;
		select @pGenreTxt = @pGenreCurrent;
	end
	else
	begin
		select @pGenreTxt = @pGenreTxt + &#039; &#124; &#039; + @pGenreCurrent;
	end
	select @priorID = @pID;
	select @priorName = @pName;
	select @priorPrice = @pPrice;
	select @priorGenreCurrent = @pGenreCurrent;
	fetch next from myCursor into @pID, @pName, @pGenreCurrent, @pPrice;
	select @pCounter = @pCounter+1;
 end;
 print @priorName + &#039; &#039; + @pGenreTxt + &#039; &#039; + @priorPrice
 close myCursor;
 deallocate myCursor;]]></description>
		<content:encoded><![CDATA[<p>The only reason I see using a with statement here is to avoid creating the tables.  I recommend a cursor for your request.</p>
<p>I&#8217;ve changed your joins to outer in case you have a title without a Genre assigned or there is an orphaned entry in Product_Genre.</p>
<p>Also, I&#8217;d change the &#8216;union all&#8217; to a simple union to reduce possible duplicity.  Now ABC is twice as horrific as you might think.  Just remove the single &#8216;all&#8217; and it will just be horror.</p>
<p>declare @priorID int, @priorName varchar(3), @priorPrice varchar(6), @priorGenreCurrent varchar(max);<br />
declare @pID int, @pName varchar(3), @pPrice varchar(6), @pGenreCurrent varchar(20), @pGenreTxt varchar(max);<br />
declare @pCounter int;</p>
<p>select @pCounter = 1;<br />
select @priorID = 0;<br />
select @priorName = null;<br />
select @priorPrice = null;<br />
select @priorGenreCurrent = null;<br />
select @pGenreTxt = null;</p>
<p>declare myCursor cursor for<br />
&#8211;If you have tables comment out the with block<br />
&#8211;Begin with block<br />
WITH<br />
 ProductMaster(Product_ID, ProductName, Price)<br />
 AS<br />
 (<br />
 SELECT 1, &#8216;XYZ&#8217;, 20.10 UNION<br />
 SELECT 2, &#8216;ABC&#8217;, 11.35 UNION<br />
 SELECT 3, &#8216;PQR&#8217;, 05.33<br />
 ),<br />
 Genre(Genre_ID, GenreName)<br />
 AS<br />
 (<br />
 SELECT 101, &#8216;Horror&#8217; UNION<br />
 SELECT 102, &#8216;Romantic&#8217; UNION<br />
 SELECT 103, &#8216;Suspense&#8217;<br />
),<br />
Product_Genre(Genre_ID, ProductID)<br />
 AS<br />
 (<br />
 SELECT 101, 1 UNION<br />
 SELECT 102, 1 UNION<br />
 SELECT 101, 3 UNION<br />
 SELECT 103, 3 UNION<br />
 SELECT 101, 2 UNION all<br />
 SELECT 101, 2<br />
 )<br />
&#8211;End with block<br />
SELECT<br />
 ProductMaster.Product_ID,<br />
 ProductMaster.ProductName,<br />
 Genre.GenreName,<br />
 ProductMaster.Price<br />
FROM<br />
 ProductMaster<br />
 left outer join Product_Genre on ProductMaster.Product_ID = Product_Genre.ProductID<br />
 left outer join Genre on Product_Genre.Genre_ID = Genre.Genre_ID<br />
 order by ProductMaster.Product_ID, Product_Genre.Genre_ID;</p>
<p> open myCursor<br />
 fetch next from myCursor into @pID, @pName, @pGenreCurrent, @pPrice;<br />
 while @@FETCH_STATUS = 0<br />
 begin<br />
	if (@priorID = 0) or (@priorID  @pID)<br />
	begin<br />
		print @priorName + &#8216; &#8216; +  @pGenreTxt + &#8216; &#8216; + @priorPrice;<br />
		select @pGenreTxt = @pGenreCurrent;<br />
	end<br />
	else<br />
	begin<br />
		select @pGenreTxt = @pGenreTxt + &#8216; | &#8216; + @pGenreCurrent;<br />
	end<br />
	select @priorID = @pID;<br />
	select @priorName = @pName;<br />
	select @priorPrice = @pPrice;<br />
	select @priorGenreCurrent = @pGenreCurrent;<br />
	fetch next from myCursor into @pID, @pName, @pGenreCurrent, @pPrice;<br />
	select @pCounter = @pCounter+1;<br />
 end;<br />
 print @priorName + &#8216; &#8216; + @pGenreTxt + &#8216; &#8216; + @priorPrice<br />
 close myCursor;<br />
 deallocate myCursor;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vicus Brits</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-304291</link>
		<dc:creator><![CDATA[Vicus Brits]]></dc:creator>
		<pubDate>Fri, 22 Jun 2012 11:09:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-304291</guid>
		<description><![CDATA[Hi Pinal, 

You are a legend, everytime I google for a T SQL problem and your website comes up -  I know my problem is solved!. 

This example was especially well presented. 

Thanks!
VB]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal, </p>
<p>You are a legend, everytime I google for a T SQL problem and your website comes up &#8211;  I know my problem is solved!. </p>
<p>This example was especially well presented. </p>
<p>Thanks!<br />
VB</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Max</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-277487</link>
		<dc:creator><![CDATA[Max]]></dc:creator>
		<pubDate>Thu, 19 Apr 2012 07:25:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-277487</guid>
		<description><![CDATA[Sorry, the correct text:

SELECT * from table
where 1=1 and @inputId = 0

union all

SELECT * from table
where 1=1 and @inputId != 0]]></description>
		<content:encoded><![CDATA[<p>Sorry, the correct text:</p>
<p>SELECT * from table<br />
where 1=1 and @inputId = 0</p>
<p>union all</p>
<p>SELECT * from table<br />
where 1=1 and @inputId != 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Max</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-277486</link>
		<dc:creator><![CDATA[Max]]></dc:creator>
		<pubDate>Thu, 19 Apr 2012 07:23:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-277486</guid>
		<description><![CDATA[Hi, you can try it so:

with cte as
(
SELECT * from table
where 1=1 and inputId = 0

union all

SELECT * from table
where 1=1 and @inputId 0
)
select * from cte]]></description>
		<content:encoded><![CDATA[<p>Hi, you can try it so:</p>
<p>with cte as<br />
(<br />
SELECT * from table<br />
where 1=1 and inputId = 0</p>
<p>union all</p>
<p>SELECT * from table<br />
where 1=1 and @inputId 0<br />
)<br />
select * from cte</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: leplep29</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-250798</link>
		<dc:creator><![CDATA[leplep29]]></dc:creator>
		<pubDate>Fri, 10 Feb 2012 04:27:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-250798</guid>
		<description><![CDATA[can you help me with this kind of query

category table
having categoryID,categoryName,parentCategory,categoryStatus

i want to have just like this output

categoryID &#124; CategoryName &#124; ParentCategory &#124; CategoryStatus
1                     Shoes                   null                           Active
2                     Shoes&gt;Air             1                            Active
3                 Shoes&gt;Air&gt;Jordan     2                            Active

Hope you might help thanks ;)]]></description>
		<content:encoded><![CDATA[<p>can you help me with this kind of query</p>
<p>category table<br />
having categoryID,categoryName,parentCategory,categoryStatus</p>
<p>i want to have just like this output</p>
<p>categoryID | CategoryName | ParentCategory | CategoryStatus<br />
1                     Shoes                   null                           Active<br />
2                     Shoes&gt;Air             1                            Active<br />
3                 Shoes&gt;Air&gt;Jordan     2                            Active</p>
<p>Hope you might help thanks ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-249749</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Wed, 08 Feb 2012 01:32:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-249749</guid>
		<description><![CDATA[[...] on CTE: Simple Example of Recursive CTE Multiple CTE in One SELECT Statement Query Common Table Expression (CTE) and Few Observation Delete Duplicate Rows Simple Example of Recursive [...]]]></description>
		<content:encoded><![CDATA[<p>[...] on CTE: Simple Example of Recursive CTE Multiple CTE in One SELECT Statement Query Common Table Expression (CTE) and Few Observation Delete Duplicate Rows Simple Example of Recursive [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Common Gotcha&#8217;s Associated with Common Table Expressions (CTE) &#8211; Quiz &#8211; Puzzle &#8211; 26 of 31 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-244326</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Common Gotcha&#8217;s Associated with Common Table Expressions (CTE) &#8211; Quiz &#8211; Puzzle &#8211; 26 of 31 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Fri, 27 Jan 2012 01:31:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-244326</guid>
		<description><![CDATA[[...] and Answers ISBN: 1466405643 Page#109-112 Common Table Expression (CTE) and Few Observation Multiple CTE in One SELECT Statement Query Delete Duplicate Rows Simple Example of Recursive CTE SQL SERVER – Simple Example of Recursive [...]]]></description>
		<content:encoded><![CDATA[<p>[...] and Answers ISBN: 1466405643 Page#109-112 Common Table Expression (CTE) and Few Observation Multiple CTE in One SELECT Statement Query Delete Duplicate Rows Simple Example of Recursive CTE SQL SERVER – Simple Example of Recursive [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sairam</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-234773</link>
		<dc:creator><![CDATA[sairam]]></dc:creator>
		<pubDate>Tue, 10 Jan 2012 11:44:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-234773</guid>
		<description><![CDATA[Hi i need clarification for this.my req. is this how should i do 
With CTE1 AS
(
)
,CTE2 AS
(
)
If(@inputId = 0)
SELECT * from CTE1
else
SELECT * from CTE2]]></description>
		<content:encoded><![CDATA[<p>Hi i need clarification for this.my req. is this how should i do<br />
With CTE1 AS<br />
(<br />
)<br />
,CTE2 AS<br />
(<br />
)<br />
If(@inputId = 0)<br />
SELECT * from CTE1<br />
else<br />
SELECT * from CTE2</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Kennedy</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-184697</link>
		<dc:creator><![CDATA[Eric Kennedy]]></dc:creator>
		<pubDate>Fri, 28 Oct 2011 19:46:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-184697</guid>
		<description><![CDATA[Still a useful article 2 years later.   I have to say, I like the elegance of method 1, but after many rounds of performance testing, I found method 2 to run faster.

Thanks for the article.  It solved my problem]]></description>
		<content:encoded><![CDATA[<p>Still a useful article 2 years later.   I have to say, I like the elegance of method 1, but after many rounds of performance testing, I found method 2 to run faster.</p>
<p>Thanks for the article.  It solved my problem</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dok</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-141633</link>
		<dc:creator><![CDATA[Dok]]></dc:creator>
		<pubDate>Fri, 17 Jun 2011 13:02:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-141633</guid>
		<description><![CDATA[Id &#124; Column1 &#124; Column2 &#124; Column3
1 &#124; 5 &#124; 5 =&gt; Same as Column1 &#124; 5 =&gt; Same as Column2
2 &#124; 2 &#124; 12 =&gt; 2+5+5 &#124; 17 =&gt; 12+5
3 &#124; 3 &#124; 32 =&gt; 3+12+17 &#124; 49 =&gt; 32+17]]></description>
		<content:encoded><![CDATA[<p>Id | Column1 | Column2 | Column3<br />
1 | 5 | 5 =&gt; Same as Column1 | 5 =&gt; Same as Column2<br />
2 | 2 | 12 =&gt; 2+5+5 | 17 =&gt; 12+5<br />
3 | 3 | 32 =&gt; 3+12+17 | 49 =&gt; 32+17</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dok</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-141632</link>
		<dc:creator><![CDATA[Dok]]></dc:creator>
		<pubDate>Fri, 17 Jun 2011 13:00:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-141632</guid>
		<description><![CDATA[Now I want to solve one problem again. 

I want to make columns in a table like below: 

Id   Column1    Column2                                                                                                        Column3
1    5                   5 =&gt; Same as Column1                                                                                5 =&gt; Same as Column2
2    2                  12 =&gt; column1 current + column2.prev + column3.previous = 2+5+5  17 =&gt; column2.current + column3.prev = 12+5
3    3                32 =&gt; 3+12+17      

easier way to see:

Id   Column1    Column2                       Column3
1    5          5 =&gt; Same as Column1     5 =&gt; Same as Column2
2    2          12 =&gt;   2+5+5                        17 =&gt; 12+5
3    3          32 =&gt;   3+12+17                    49 =&gt; 32+17

I am looking forward any answer and it will be appreciated. Thank you in advance

Dok]]></description>
		<content:encoded><![CDATA[<p>Now I want to solve one problem again. </p>
<p>I want to make columns in a table like below: </p>
<p>Id   Column1    Column2                                                                                                        Column3<br />
1    5                   5 =&gt; Same as Column1                                                                                5 =&gt; Same as Column2<br />
2    2                  12 =&gt; column1 current + column2.prev + column3.previous = 2+5+5  17 =&gt; column2.current + column3.prev = 12+5<br />
3    3                32 =&gt; 3+12+17      </p>
<p>easier way to see:</p>
<p>Id   Column1    Column2                       Column3<br />
1    5          5 =&gt; Same as Column1     5 =&gt; Same as Column2<br />
2    2          12 =&gt;   2+5+5                        17 =&gt; 12+5<br />
3    3          32 =&gt;   3+12+17                    49 =&gt; 32+17</p>
<p>I am looking forward any answer and it will be appreciated. Thank you in advance</p>
<p>Dok</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dok</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-141624</link>
		<dc:creator><![CDATA[Dok]]></dc:creator>
		<pubDate>Fri, 17 Jun 2011 11:53:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-141624</guid>
		<description><![CDATA[thank you, this post helped me to solve one problem. I am happy
Good luck 

Dok]]></description>
		<content:encoded><![CDATA[<p>thank you, this post helped me to solve one problem. I am happy<br />
Good luck </p>
<p>Dok</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: P Sri Sagar</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-138379</link>
		<dc:creator><![CDATA[P Sri Sagar]]></dc:creator>
		<pubDate>Thu, 02 Jun 2011 10:05:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-138379</guid>
		<description><![CDATA[Hi,

I have small doubt. The results from the above query and for the below query were same.

Then why i have to use CTE. Can you explain? Actually, i am the beginner for CTE&#039;s.


select ProductMaster.Product_ID,
ProductMaster.ProductName,
Genre.GenreName,
ProductMaster.Price
 from ProductMaster PM
Inner Join Product_Genre P_G on PM.Product_Id = P_G.Productid
Inner Join Genre G On G.Genre_Id = P_G.Genre_Id


Waiting for your reply,

Thanks &amp; Regards
Sri Sagar. P]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I have small doubt. The results from the above query and for the below query were same.</p>
<p>Then why i have to use CTE. Can you explain? Actually, i am the beginner for CTE&#8217;s.</p>
<p>select ProductMaster.Product_ID,<br />
ProductMaster.ProductName,<br />
Genre.GenreName,<br />
ProductMaster.Price<br />
 from ProductMaster PM<br />
Inner Join Product_Genre P_G on PM.Product_Id = P_G.Productid<br />
Inner Join Genre G On G.Genre_Id = P_G.Genre_Id</p>
<p>Waiting for your reply,</p>
<p>Thanks &amp; Regards<br />
Sri Sagar. P</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER – Common Table Expression (CTE) and Few Observation Journey to SQLAuthority</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-133695</link>
		<dc:creator><![CDATA[SQL SERVER – Common Table Expression (CTE) and Few Observation Journey to SQLAuthority]]></dc:creator>
		<pubDate>Tue, 10 May 2011 02:17:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-133695</guid>
		<description><![CDATA[[...] SQL Server – Multiple CTE in One SELECT Statement Query [...]]]></description>
		<content:encoded><![CDATA[<p>[...] SQL Server – Multiple CTE in One SELECT Statement Query [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anand</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-129435</link>
		<dc:creator><![CDATA[Anand]]></dc:creator>
		<pubDate>Mon, 18 Apr 2011 03:34:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-129435</guid>
		<description><![CDATA[Hi Pinal,

Is there any level for recursive CTE? How many levels we can go?

Please reply.

Thanks
Anand]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>Is there any level for recursive CTE? How many levels we can go?</p>
<p>Please reply.</p>
<p>Thanks<br />
Anand</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-118645</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Tue, 15 Feb 2011 15:44:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-118645</guid>
		<description><![CDATA[Search for Delete duplicates in this site]]></description>
		<content:encoded><![CDATA[<p>Search for Delete duplicates in this site</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brijesh Bellur</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comment-117824</link>
		<dc:creator><![CDATA[Brijesh Bellur]]></dc:creator>
		<pubDate>Thu, 10 Feb 2011 04:01:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399#comment-117824</guid>
		<description><![CDATA[i want to remove duplicate and info is linked in different table.

for eg. if customer name, add store in one table then same customer phone no. in phone table.
so if customer name,add and phone no. are twise delete the record.



thanx madhivanan for the support

waiting for result]]></description>
		<content:encoded><![CDATA[<p>i want to remove duplicate and info is linked in different table.</p>
<p>for eg. if customer name, add store in one table then same customer phone no. in phone table.<br />
so if customer name,add and phone no. are twise delete the record.</p>
<p>thanx madhivanan for the support</p>
<p>waiting for result</p>
]]></content:encoded>
	</item>
</channel>
</rss>
