November 6, 2007 at 1:51 pm
Hi sql gurus,
I have a table that has product name and submissionID and some other columns. I am really concerned about these two columns. My task is to get all the submissionIDs for a particular product name and display SubmissionIDs seperated by commas against each product name .
The tables below might give a better idea
current scenario:
Product Name SubmissionID columnC Column D
AAA 123
AAA 456
BBB 111
ccc 121
AAA 789
Expected result:
Product Name SubmissionID columnC Column D
AAA 123,456,789
BBB 111
CCC 121
Any suggestions/pointers would be highly appreciated.
Alicia Rose
November 6, 2007 at 8:59 pm
If you have a table that looks kinda like this called "yourtable"...
CREATE TABLE yourtable (ProductName VARCHAR(10), SubmissionID INT)
INSERT INTO yourtable
(ProductName,SubmissionID)
SELECT 'AAA',123 UNION ALL
SELECT 'AAA',456 UNION ALL
SELECT 'BBB',111 UNION ALL
SELECT 'ccc',121 UNION ALL
SELECT 'AAA',789
...then, create a function that looks like this...
CREATE FUNCTION dbo.fnConcatSubmissionID (@ProductName AS VARCHAR(10))
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Return VARCHAR(8000)
SELECT @Return = ISNULL(@Return +',' , '') + CAST(SubmissionID AS VARCHAR(10))
FROM yourtable
WHERE ProductName = @ProductName
RETURN @Return
END
GO
... and do a SELECT like this...
SELECT DISTINCT
ProductName,
dbo.fnConcatSubmissionID(ProductName) AS SubmissionIDs
FROM yourtable
ORDER BY ProductName
... and, yes, you can add almost as many columns as you'd like to the SELECT list...
--Jeff Moden
Change is inevitable... Change for the better is not.
November 12, 2007 at 3:11 pm
Hi Jeff,
Thanks a lot for the reply.
I am in a similar situation again.
I have a table that has columns 1)ProductName and 2)qualification.
The reporting scenario is like i have to display the highest qualification among all qualifications for the particular product name:
Suppose 3 qualifications(B,P,D)
D__lowest
B__Medium
P__highest
So i need P always if P is there among the group.
The different conditions would be
1)If the ProductName has only only type of qualification then display that.
2)If both B and D, then display only B.
3)If both P and D, then display only p.
4) If p,B and D then display only P.
The current table would be like:
ProductName QualificationCol3Col4
P1B
P1P
P1D
P2B
P2D
P3P
P3D
P4D
P5B
P5B
P5B
The expected Result:
ProductName QualificationCol3Col4
P1P
P2B
P3P
P4D
P5B
Any pointers would be highly appreciated
Alicia Rose
November 12, 2007 at 4:20 pm
I'm not sure how that is similar, but here's this should be continued in your other post here:
http://www.sqlservercentral.com/Forums/Topic421242-338-1.aspx
November 12, 2007 at 6:00 pm
I don't see any similarity, either, except for the fact that Alicia has, again, not shown anything that she's tried (not to mention the double post).
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply