我的psql数据库col中有pat_first_name和pat_last_name列。我想挑出最常见的名字。我可以使用
做名字SELECT pat_first_name,count(pat_first_name)
from patients
GROUP BY pat_first_name
ORDER BY count DESC;`
然而,当我尝试结合它失败时
SELECT pat_first_name,pat_last_name,count((pat_first_name || ' ' || pat_last_name))
from patients
GROUP BY (pat_first_name || ' ' || pat_last_name)
ORDER BY count DESC;
列“patients.pat_first_name”必须出现在GROUP BY子句中或用于聚合函数
我哪里错了?
答案 0 :(得分:2)
你可以这样做:
SELECT pat_first_name,pat_last_name,COUNT(*)
FROM patients
GROUP BY pat_first_name,pat_last_name
ORDER BY COUNT(*) DESC;
或者,如果您确实希望在结果中连接first_name和last_name:
SELECT pat_first_name || ' ' || pat_last_name,COUNT(*)
FROM patients
GROUP BY pat_first_name || ' ' || pat_last_name
ORDER BY COUNT(*) DESC;
无论哪种方式,你都必须在SELECT中拥有与GROUP BY中相同的“非计数”列。