TSQL,计算表中的值对

时间:2010-10-14 15:05:26

标签: sql tsql

给出格式为

的表格
ID   Forename    Surname
1    John        Doe
2    Jane        Doe
3    Bob         Smith
4    John        Doe

你将如何获得输出

Forename  Surname  Count
John      Doe      2
Jane      Doe      1
Bob       Smith    1

对于单个列我只会使用count,但我不确定如何将其应用于多个。

2 个答案:

答案 0 :(得分:9)

SELECT Forename, Surname, COUNT(*) FROM YourTable GROUP BY Forename, Surname

答案 1 :(得分:1)

我认为这应该有效:

SELECT Forename, Surname, COUNT(1) AS Num 
FROM T
GROUP BY Forename, Surname