SQL 相同国家、相同姓名、相同姓氏(例如史密斯)的人的年龄相同

SQL Count of same country, same name, same age for people with same last name (e.g smith)

我需要你的建议。

我们有以下table

table 的姓名:PEOPLE(我知道不好的名字,例如 :-))

ID       | firstname | age | country
1        | George    | 20  | US
1        | George    | 20  | GB
2        | Jim       | 20  | FR
2        | Jim       | 21  | FR

我需要查看以下结果,因此对于相同 ID 的值计数

ID | firstnamecnt | agecnt | countrycnt
1  | 2            | 2      | 1
2  | 2            | 1      | 2

希望我解释得很好:-)

感谢和问候, 亚历克斯

我假设你指的是名字。这是一种方法

select id, 
       count(*) - count(distinct firstname) + 1 as firstnamecnt,
       count(*) - count(distinct age) + 1 as agecnt,
       count(*) - count(distinct country) + 1 as countrycnt
from people
group by id;

DEMO