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


Get distinct values into single column


Get distinct values into single column

Author
Message
Triality
Triality
Old Hand
Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)

Group: General Forum Members
Points: 356 Visits: 270
Hi -

I am having trouble writing a query to get the distinct values of a column grouping into a single column. Please see the example below:

Data:

Store Name | Category | Product | Color

My Store | Clothing | Awesome Shirt | White
My Store | Clothing | Awesome Shirt | Green
My Store | Clothing | Awesome Shirt | Blue

My Store | Cars | Chevy Impala | White
My Store | Cars | Chevy Impala | Black
My Store | Cars | Chevy Impala | Grey

Desired Result:

My Store | Clothing | Awesome Shirt | WhiteGreenBlue
My Store | Cars | Chevy Impala | WhiteBlackGrey

Thanks for your help!
BriPan
BriPan
SSC-Addicted
SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)

Group: General Forum Members
Points: 421 Visits: 296
try this link
http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/rowset-string-concatenation-which-method-is-best.aspx
Paul White
Paul White
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35394 Visits: 11361
SQLFiddle (link) showing the following solution:


DECLARE @Data AS TABLE
(
StoreName varchar(50) NOT NULL,
Category varchar(50) NOT NULL,
Product varchar(50) NOT NULL,
Color varchar(50) NOT NULL,

PRIMARY KEY (StoreName, Category, Product, Color)
);

INSERT @Data
(StoreName, Category, Product, Color)
VALUES
('My Store', 'Clothing', 'Awesome Shirt', 'White'),
('My Store', 'Clothing', 'Awesome Shirt', 'Green'),
('My Store', 'Clothing', 'Awesome Shirt', 'Blue'),
('My Store', 'Cars', 'Chevy Impala', 'White'),
('My Store', 'Cars', 'Chevy Impala', 'Black'),
('My Store', 'Cars', 'Chevy Impala', 'Grey');


SELECT
d.StoreName,
d.Category,
d.Product,
Colors =
(
SELECT
d2.Color AS [text()]
FROM @Data AS d2
WHERE
d2.StoreName = d.StoreName
AND d2.Category = d.Category
AND d2.Product = d.Product
ORDER BY
d2.Color
FOR XML
PATH (''), TYPE
).value('.[1]', 'varchar(MAX)')
FROM @Data AS d
GROUP BY
d.StoreName,
d.Category,
d.Product
ORDER BY
d.StoreName,
d.Category,
d.Product;





Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Triality
Triality
Old Hand
Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)

Group: General Forum Members
Points: 356 Visits: 270
Thank you both, this is very helpful!
Paul White
Paul White
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35394 Visits: 11361
dgowrij (2/19/2013)
Could you please clarify what the below statement actually does?
.value('.[1]', 'varchar(MAX)')


Especially the '.[1]'

The value XML method is documented in Books Online:

http://msdn.microsoft.com/en-us/library/ms178030.aspx

The '.[1]' is an XQuery expression, where the '.' is a path expression containing an axis step and a node test:

http://msdn.microsoft.com/en-us/library/ms190451.aspx

More precisely the '.' is approximate shorthand for the axis step and node test, self::node():

http://msdn.microsoft.com/en-us/library/ms191460.aspx

The [1] is a predicate:

http://msdn.microsoft.com/en-us/library/ms177470.aspx

The XML value method requires a single value as input, hence the technical need for the predicate [1].



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
buddy__a
buddy__a
Right there with Babe
Right there with Babe (726 reputation)Right there with Babe (726 reputation)Right there with Babe (726 reputation)Right there with Babe (726 reputation)Right there with Babe (726 reputation)Right there with Babe (726 reputation)Right there with Babe (726 reputation)Right there with Babe (726 reputation)

Group: General Forum Members
Points: 726 Visits: 257
One possible modification that you might want to make is to change this:

d2.Color AS [text()]



to this:

d2.Color AS [data()]



That way your items would have a space between them instead of being run together in a continuous string.



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