SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


combine multiple rows into one dynamically query question


combine multiple rows into one dynamically query question

Author
Message
sqlblue
sqlblue
Mr or Mrs. 500
Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)

Group: General Forum Members
Points: 552 Visits: 295
Hi, I have the below table, and I need to combine them
into one records dynamically (I'm not sure I'm clear on this,
so maybe you look at the below table is better).
create table Test
(
id int,
name varchar(30),
counts int
)

insert into Test
select 1, 'Math', 5
union
select 1, 'Literature', 3
union
select 1, 'Cooking', 1
union
select 1, 'Reading', 2

I would like the result to look like this below,
id categorycounts
1 Cooking (1), Literature (3), Math(5), Reading(2)

the challenge I have is the id can have multiple values like this, how do I know to combine them all dynamically and correctly. thanks a lot if you could help and give any input.
sqlblue
sqlblue
Mr or Mrs. 500
Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)Mr or Mrs. 500 (552 reputation)

Group: General Forum Members
Points: 552 Visits: 295
Never mind, i got it.
btio_3000
btio_3000
SSC-Addicted
SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)

Group: General Forum Members
Points: 497 Visits: 493
Yes, but can you - or anyone - share your wisdom?

Let's say there is a table like below

KeyField..............DescField
1....................... 'A'
1....................... 'B'
1....................... 'C'
2.......................'D'
2.......................'E'
2.......................'F'

and I need a single select returning

1.........'A,B,C'
2.........'D,E,F'

?


Thank you
btio_3000
btio_3000
SSC-Addicted
SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)SSC-Addicted (497 reputation)

Group: General Forum Members
Points: 497 Visits: 493
lol, nevermind it too)))))))))))))
C.K.Shaiju
C.K.Shaiju
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1024 Visits: 711
Hi,

Try this...
--------------------------------------------------------
DECLARE @lValueStr VARCHAR(MAX)
SET @lValueStr = NULL
SELECT @lValueStr = COALESCE(@lValueStr + ', ', '')
+ name + '(' + CAST(Counts AS VARCHAR(5)) + ')'
FROM Test T
SELECT @lValueStr
---------------------------------------------------------

Shaiju C.K.

_____________________________________________
One ounce of practice is more important than tonnes of dreams
steve-893342
steve-893342
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1227 Visits: 2670
I had already done a PIVOT when I saw you'd already got it!

;WITH ctePrepare
AS
(SELECT id, name, name + ' (' + CONVERT(VARCHAR, counts) + ')' AS NameWithCount FROM test)
SELECT id, Cooking + ', ' + Literature + ', ' + Math + ', ' + Reading AS categorycounts
FROM
(SELECT id, name, NameWithCount FROM ctePrepare) AS Z
PIVOT (MAX(NameWithCount) FOR name IN (Cooking, Literature, Math, Reading)) AS pvt


ColdCoffee
ColdCoffee
SSCrazy Eights
SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)

Group: General Forum Members
Points: 9065 Visits: 5555
This might be helpful for some who accidentally tumbles upon this thread :-D

IF OBJECT_ID('TEMPDB..#CONCAT_COLUMN_VALUES') IS NOT NULL
DROP TABLE #CONCAT_COLUMN_VALUES

CREATE TABLE #CONCAT_COLUMN_VALUES
(
GROUP_ID INT,
COL_VAL VARCHAR(5)
)

INSERT INTO #CONCAT_COLUMN_VALUES
SELECT 1, 'A' UNION ALL
SELECT 1, 'B' UNION ALL
SELECT 1, 'C' UNION ALL
SELECT 2, 'D' UNION ALL
SELECT 2, 'E' UNION ALL
SELECT 3, 'F' UNION ALL
SELECT 4, 'G'

SELECT * FROM #CONCAT_COLUMN_VALUES

SELECT p1.GROUP_ID,
( SELECT COL_VAL + ','
FROM #CONCAT_COLUMN_VALUES p2
WHERE p2.GROUP_ID = p1.GROUP_ID
ORDER BY COL_VAL
FOR XML PATH('')
) AS Concat_Values
FROM #CONCAT_COLUMN_VALUES p1
GROUP BY p1.GROUP_ID ;



Cheers!!
Earl Downs
Earl Downs
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 83
Thanks to everybody!!!; I was testing this script in SQL Server 2005 and everthing is ok.

//>>>>
DECLARE @lValueStr VARCHAR(MAX)
SET @lValueStr = NULL

SELECT @lValueStr = COALESCE(@lValueStr + ', ', '')
+ a.Cod_Asignatura + '(' + CAST(rtrim(ltrim(b.Des_Asignatura)) AS VARCHAR(30)) + ')'
FROM pla_tt_PlanificacionDetAsig a inner join pla_tc_Asignatura b
on a.cod_empresa = b.cod_empresa
and a.cod_recinto = b.cod_recinto
and a.cod_asignatura = b.cod_asignatura

SELECT @lValueStr
<<<<//

I Really don't understan how it work, but just work and good! :-) w00t


Best regards.
caroline.keihn
caroline.keihn
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 202
Hello,

I tried this code in SQL 2000 but get this error: "Incorrect syntax near the keyword 'FOR'." Why is that?

IF OBJECT_ID('TEMPDB..#IF_BestPriceMfg2') IS NOT NULL
DROP TABLE #IF_BestPriceMfg2

CREATE TABLE #IF_BestPriceMfg2
(
GlobalCustomer INT,
Productline_ID VARCHAR(6)
)

INSERT INTO #IF_BestPriceMfg2
SELECT GlobalCustomer
,Productline_ID
FROM #IF_BestPriceMfg
GROUP BY GlobalCustomer
,Productline_ID

SELECT * FROM #IF_BestPriceMfg2

SELECT p1.GlobalCustomer,
( SELECT Productline_ID + ','
FROM #IF_BestPriceMfg2 p2
WHERE p2.GlobalCustomer = p1.GlobalCustomer
ORDER BY Productline_ID
FOR XML PATH('')
) AS Concat_Values
FROM #IF_BestPriceMfg2 p1
GROUP BY p1.GlobalCustomer ;
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2239 Visits: 1119
For XML Path is not supported in sql 2000.
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