October 13, 2010 at 4:13 am
Hi Experts,
Here we go. I have below DDL,DML and what I expecting.
create table langdtl(id int,speak_flag int,read_flag int,ia_flag int,lit1_flag int,lit2_flag int,lit3_flag int,lang nvarchar(2))
insert into langdtl values(1,1,1,1,1,1,0,'fi')
insert into langdtl values(1,1,1,0,0,0,1,'sv')
insert into langdtl values(1,1,1,1,0,1,0,'en')
insert into langdtl values(1,1,1,0,1,1,1,'de')
insert into langdtl values(2,1,1,0,0,1,1,'fi')
insert into langdtl values(2,1,0,1,1,1,0,'sv')
insert into langdtl values(2,1,1,0,1,0,1,'en')
select * from langdtl
I need output data as shown below
id speak_flag read_flag ia_flag lit1_flag lit2_flag lit3_flag
-------------------------------------------------------------------------
1 de,en,fi,sv de,en,fi,sv en,fi de,fi de,en,fi de,sv
2 de,en,fi,sv en,fi sv en,sv fi,sv en,fi
lang should have to display in alphabetical order separated by comma.
Thanks!
Bhushan
October 13, 2010 at 7:22 am
Does this meet your requirements?
SELECT DISTINCT
id,
speak_flag = STUFF((SELECT ',' + lang
FROM langdtl
WHERE id = l.id
AND speak_flag = 1
ORDER BY lang
FOR XML PATH(''),TYPE).value('.', 'varchar(max)'),1,1,''),
read_flag = STUFF((SELECT ',' + lang
FROM langdtl
WHERE id = l.id
AND read_flag = 1
ORDER BY lang
FOR XML PATH(''),TYPE).value('.', 'varchar(max)'),1,1,''),
ia_flag = STUFF((SELECT ',' + lang
FROM langdtl
WHERE id = l.id
AND ia_flag = 1
ORDER BY lang
FOR XML PATH(''),TYPE).value('.', 'varchar(max)'),1,1,''),
lit1_flag = STUFF((SELECT ',' + lang
FROM langdtl
WHERE id = l.id
AND lit1_flag = 1
ORDER BY lang
FOR XML PATH(''),TYPE).value('.', 'varchar(max)'),1,1,''),
lit2_flag = STUFF((SELECT ',' + lang
FROM langdtl
WHERE id = l.id
AND lit2_flag = 1
ORDER BY lang
FOR XML PATH(''),TYPE).value('.', 'varchar(max)'),1,1,''),
lit3_flag = STUFF((SELECT ',' + lang
FROM langdtl
WHERE id = l.id
AND lit3_flag = 1
ORDER BY lang
FOR XML PATH(''),TYPE).value('.', 'varchar(max)'),1,1,'')
FROM langdtl l
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
October 13, 2010 at 8:00 am
Heh you beat me to it Wayne, and yours is nicer too.
SELECT d.ID,
speak_flag = REPLACE(iTVF1.RowsToString + '$', ',$', ''),
read_flag = REPLACE(iTVF1.RowsToString + '$', ',$', ''),
ia_flag = REPLACE(iTVF3.RowsToString + '$', ',$', ''),
lit1_flag = REPLACE(iTVF4.RowsToString + '$', ',$', '')
-- etc etc
FROM (SELECT ID FROM langdtl GROUP BY ID) d
CROSS APPLY (SELECT lang + ','
FROM langdtl WHERE ID = d.ID AND speak_flag = 1
FOR XML PATH('')) iTVF1 (RowsToString)
CROSS APPLY (SELECT lang + ','
FROM langdtl WHERE ID = d.ID AND read_flag = 1
FOR XML PATH('')) iTVF2 (RowsToString)
CROSS APPLY (SELECT lang + ','
FROM langdtl WHERE ID = d.ID AND ia_flag = 1
FOR XML PATH('')) iTVF3 (RowsToString)
CROSS APPLY (SELECT lang + ','
FROM langdtl WHERE ID = d.ID AND lit1_flag = 1
FOR XML PATH('')) iTVF4 (RowsToString)
-- etc etc
ORDER BY ID
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 13, 2010 at 8:02 am
thanks for your reply!!!
October 13, 2010 at 9:58 am
bhushan.bagul (10/13/2010)
thanks for your reply!!!
Glad to be of help. Does it work right for you?
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
October 24, 2010 at 8:00 pm
WayneS (10/13/2010)
bhushan.bagul (10/13/2010)
thanks for your reply!!!Glad to be of help. Does it work right for you?
Heh... don'cha just love it?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 24, 2010 at 8:29 pm
Jeff Moden (10/24/2010)
WayneS (10/13/2010)
bhushan.bagul (10/13/2010)
thanks for your reply!!!Glad to be of help. Does it work right for you?
Heh... don'cha just love it?
Exercising my brain: yes.
Lack of feedback: no.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply