Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Help!!!

Status
Not open for further replies.

happyIndian100

Programmer
Jul 20, 2003
66
US
I have a TableA with the following data:
------------
Col1 Col2
-----------
A A1
A A1
A A1
B B1
B B2
C C1
C C2
C C3
D D1

i need to get this Query Result:

A A1,A1,A1
B B1,B2
C C1,C2,C3
D D1

Thanks in Advance:)
 
I had a similar problem. Here's what you need to do:
Code:
CREATE FUNCTION concat_values
(
	@column_1 VARCHAR(255)
)
RETURNS VARCHAR(8000)
AS
BEGIN
	DECLARE @result VARCHAR(8000)

	SET @result = ''
	SELECT @result = @result + col_2 + ','
	FROM table_a
	WHERE col_1 = @column_1
	ORDER BY col_2
	
	IF RIGHT(@result, 2) = ','
		SELECT @result = LEFT(@result, LEN(@result) - 2)

	Return @result
END

Then run the following query:
Code:
SELECT col_1
	,concat_values(col_1)
FROM table_a
ORDER BY col_1

I haven't tested this code, but it should get you in the right direction.

Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top