sql server中的字符数

时间:2009-09-30 06:16:54

标签: sql sql-server tsql

输入:AA1

输出将是:

Char  Count
A     2
1     1

1 个答案:

答案 0 :(得分:2)

这更容易,因为它只是简单的计数,而不是你的其他问题中的连续性。

DECLARE @Numbers TABLE (Num smallint NOT NULL PRIMARY KEY)

INSERT @Numbers (Num)
SELECT TOP 12
    ROW_NUMBER() OVER (ORDER BY c1.NAME)
FROM
    sys.columns c1

DECLARE @STuff TABLE (Seq char(12) NOT NULL PRIMARY KEY)

INSERT @STuff (Seq) VALUES ('12AABCCDDDDE')
INSERT @STuff (Seq) VALUES ('333345RRRR11')

SELECT
   Seq, Single, COUNT(*)
FROM
   @Numbers N
   CROSS APPLY 
   (SELECT Seq, SUBSTRING(Seq, Num, 1) AS Single FROM @Stuff) S
GROUP BY
   Seq, Single
HAVING
    COUNT(*) > 1
ORDER BY
    Seq, Single