Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CTE


Author
Message
sunitha.yanagandala
sunitha.yanagandala
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 32
Input:
Deptno Empname
10 Niladri
20 Jeeva
30 Deepak
30 Prantik
20 Arina

Output:

Deptno Empname
10 Niladri
20 Jeeva,Arina
30 Deepak,Prantik

I have used CTE for this

WITH
cteReports(Deptno,Empname)
AS
(
SELECT TOP 1 Deptno,Empname FROM #tblemp
UNION ALL
SELECT e.Deptno,e.Empname + ',' + r.Empname
FROM #tblemp e
INNER JOIN cteReports r
ON e.Deptno = r.Deptno
)
SELECT * FROM cteReports
ORDER BY Deptno

But I am getting Error
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47402 Visits: 44399
What error are you getting?


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


sunitha.yanagandala
sunitha.yanagandala
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 32
Incorrect syntax near 'cteReports'.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47402 Visits: 44399
Can't see anything wrong.

Make sure that the statement before the CTE (if there is one) is terminated with a ;


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


twin.devil
twin.devil
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1378 Visits: 2598
CTE will not server the purpose, you will be getting MaxRecursion error because there is no particular hierarchy is available in your table.

following query is written with respect for your desired output:

create table #tblemp (Deptno int, Empname nvarchar(50))

insert into #tblemp
select 10,'Niladri' union all
select 20,'Jeeva' union all
select 30,'Deepak' union all
select 30,'Prantik' union all
select 20,'Arina'

;With cte
AS
(
Select Distinct Deptno
from #tblemp
)


Select e.Deptno , Stuff( (select ', ' + Empname
from #tblemp EE
Where e.Deptno = ee.Deptno
for xml Path ('')),1,2,'')

from cte E


Drop table #tblemp



Hope it helps in case of any query do let us know.
sunitha.yanagandala
sunitha.yanagandala
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 32
Thanks twin.devil
It works fine
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