SQL: how to get all the distinct characters in a column, across all rows

后端 未结 5 444
旧巷少年郎
旧巷少年郎 2020-12-30 03:39

Is there an elegant way in SQL Server to find all the distinct characters in a single varchar(50) column, across all rows?

Bonus points if it can be done without cur

5条回答
  •  生来不讨喜
    2020-12-30 04:18

    Here's a query that returns each character as a separate row, along with the number of occurrences. Assuming your table is called 'Products'

    WITH ProductChars(aChar, remain) AS (
       SELECT LEFT(productName,1), RIGHT(productName, LEN(productName)-1) 
          FROM Products WHERE LEN(productName)>0
       UNION ALL
       SELECT LEFT(remain,1), RIGHT(remain, LEN(remain)-1) FROM ProductChars
          WHERE LEN(remain)>0
    )
    SELECT aChar, COUNT(*) FROM ProductChars
    GROUP BY aChar
    

    To combine them all to a single row, (as stated in the question), change the final SELECT to

    SELECT aChar AS [text()] FROM
      (SELECT DISTINCT aChar FROM ProductChars) base
    FOR XML PATH('')
    

    The above uses a nice hack I found here, which emulates the GROUP_CONCAT from MySQL.

    The first level of recursion is unrolled so that the query doesn't return empty strings in the output.

提交回复
热议问题