开发者

TSQL, counting pairs of values in a table

开发者 https://www.devze.com 2023-01-19 20:32 出处:网络
Given a table in the format of IDForenameSurname 1JohnDoe 2Jane开发者_如何学运维 Doe 3BobSmith 4JohnDoe

Given a table in the format of

ID   Forename    Surname
1    John        Doe
2    Jane       开发者_如何学运维 Doe
3    Bob         Smith
4    John        Doe

How would you go about getting the output

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

For a single column I would just use count, but am unsure how to apply that for multiple ones.


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


I think this should work:

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

精彩评论

暂无评论...
验证码 换一张
取 消