我是SQL的新手,如果以前曾问过这个问题,我很抱歉 - 我无法正确地说出来。
说我有一个看起来像这样的表:
Name Call ID
Sally 1
Sally 2
Sally 3
Mike 4
Mike 5
Bob 6
Bob 7
我想创建一个如下所示的新表:
Name No. of calls
Sally 3
Mike 2
Bob 2
尝试
我想我会做类似的事情:
SELECT
Name,
COUNT(distinct Name) AS No. of Calls
FROM Table
感谢。
答案 0 :(得分:2)
你只需要将它们分组即可。
SELECT
Name
COUNT(*) AS [No. of Calls]
FROM
Table
GROUP BY
Name
答案 1 :(得分:2)
您正在寻找GROUP BY
:
SELECT Name, COUNT(*) AS 'No. of Calls'
FROM Table
GROUP BY NAME
ORDER BY COUNT(*) DESC