0

I have pat_first_name and pat_last_name columns in my psql database col. Id like to pick out most common names. I can do first names using

SELECT pat_first_name,count(pat_first_name) 
from patients 
GROUP BY pat_first_name 
ORDER BY count DESC;`

However, when I try combine it fails

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;

column "patients.pat_first_name" must appear in the GROUP BY clause or be used in an aggregate function

where am I going wrong?

1 Answer 1

3

You can just do:

SELECT pat_first_name,pat_last_name,COUNT(*) 
FROM patients 
GROUP BY pat_first_name,pat_last_name
ORDER BY COUNT(*) DESC;

Or, if you really want the first_name and last_name concatenated in the result:

SELECT pat_first_name || ' ' || pat_last_name,COUNT(*) 
FROM patients 
GROUP BY pat_first_name || ' ' || pat_last_name
ORDER BY COUNT(*) DESC;

Either way, you gotta have the same "non-count" columns in the SELECT as in the GROUP BY.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.