Using this page: Can I Comma Delimit Multiple Rows Into One Column?
... I've been able to come up with results such as this:
Using this query:
SELECT
[Client ID],
STUFF((SELECT ', ' + [Location (counts)]
FROM (
SELECT TOP 100 PERCENT [Client ID]
, ltrim(str([Store Num])) + ' (' + ltrim(str(count([Store Num]))) + ') ' [Location (counts)]
FROM @tbl_coreData
GROUP BY [Store Num], [Client ID]
ORDER BY COUNT([Store Num]) DESC) tblThis
WHERE ([Store Num] = Result.[Store Num]
AND [Client ID] = Result.[Client ID])
FOR XML PATH ('')),1,1,'') AS BATCHNOLIST
FROM @tbl_coreData AS Result
GROUP BY [Client ID], [Store Num]
I want the list to be ordered by The count of occurrences (the number in parenthesis). My attempt at this was the
SELECT TOP 100 PERCENT
[Client ID]
and
ORDER BY COUNT([Store Num]) DESC
but this didn't work.
How can I make the concatenated values be presented in order of descending occurrence?
As always, all help is welcome and appreciated.