May 15, 2006 at 11:15 pm
Hi all,
I have the following table:
Table1([ID] int,[Name] varchar(255))
This table simply stores a unique ID for each row and a name. The name CAN be duplicated for different records, so for example, ID's 1 and 2 can both have the name 'Test'.
Now, what I want is to create a query that will return a table with two columns: Table2([Name] varchar(255), [ID_List] varchar(255))
I want this table to display all distinct [Name]s in one column and a list of IDs in the next column which have that name. So for example, from the same data discussed above for Table1, Table2 will have: ('Test', '1,2').
So basically I want to have the ID's concatenated into a single string stored in the second column.
Anyone have a clue how to do this?
Thanks
Paul
May 16, 2006 at 12:16 am
There must be dosen clues just in this forum for the last week.
Would you bother to browse forum first?
_____________
Code for TallyGenerator
May 16, 2006 at 12:29 am
If I could find what you're referring to, then I wouldn't post my message. Obviously I did not find it.
Thanks for the lecture anyway.
May 16, 2006 at 4:12 am
do you want that in single query?
This cabe easily be achived by using cursors..
Thanks
Brij
May 16, 2006 at 4:23 am
Declare @Name Varchar(255)
Declare @ID_LIST Varchar(255)
Declare @Temp Table
(
Name varchar(255),
ID_LIST Varchar(255)
)
Declare cur1 scroll cursor for Select name from table1 group by Name
Open cur1
Fetch cur1 into @Name while @@Fetch_Status =0
Begin
Set @ID_LIST=null
Select @ID_LIST = COALESCE(@ID_LIST + ', ', '') + CAST(ID as varchar) FROM Table1 where name =@Name
Insert into @Temp Values (@Name, @ID_LIST)
Fetch cur1 into @Name
End
Close cur1
Deallocate cur1
Select * from @temp
Brij
May 16, 2006 at 6:27 am
We should probably look into creating a sticky post with a FAQ. This question, in particular, seems to come up two or three times a week.
CREATE FUNCTION ConcatID (@vName varchar(50))
RETURNS varchar(50)
AS
BEGIN
DECLARE @myString varchar(50)
SET @myString = ''
SELECT @myString = @myString + CAST(Table1.ID AS Varchar(10)) + ','
FROM Table1
WHERE Table1.[Name] = @vName
RETURN @myString
END
SELECT [Name], ConcatName([Name]) AS IDList
FROM Table1
GROUP BY [Name]
May 16, 2006 at 6:35 am
![]() | There must be dosen clues just in this forum for the last week. |
Yep and even longer than that
But finding them isn't easy
Marshall beat me to this, but my solution anyway
CREATE FUNCTION dbo.udf_test (@Name varchar(255))
RETURNS varchar(255)
AS
BEGIN
DECLARE @result varchar(255)
SELECT @result = COALESCE(@result + ',' + CAST([ID] as varchar), CAST([ID] as varchar))
FROM [Table1]
WHERE [Name] = @Name
ORDER BY [ID]
RETURN @result
END
SELECT [Name], dbo.udf_test([Name])
FROM (SELECT [Name] FROM [Table1] GROUP BY [Name]) a
ORDER BY [Name] ASC
Far away is close at hand in the images of elsewhere.
Anon.
May 17, 2006 at 9:30 am
SELECT [Name], dbo.udf_test([Name])
FROM (SELECT [Name] FROM [Table1] GROUP BY [Name]) a
ORDER BY [Name] ASC
David,
Why did you do an Alias here? You didn't have a reference to it in the select. Is this necessary, or just good general syntax?
May 17, 2006 at 9:47 am
![]() | Why did you do an Alias here? |
SQL Server requires sub queries to be aliased, you will get an error otherwise
Far away is close at hand in the images of elsewhere.
Anon.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy