In Oracle SQL, how do I return count(*) column along side of a regular column?
select count(*) from TABLE where username = 'USERNAME';
select username,count(*) from TABLE where username = 'USERNAME';
So I want the username along side of the count, to expand this into another query that lists numerous usernames and the count of their records.
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
*Cause:
*Action:
Error at Line: 7 Column: 7
So, how do I do it?
SELECT username,count(*) from TABLE WHERE username='USERNAME' GROUP BY username
should do the trick!
The reason the first query works is because MySQL can automatically convert that query into an aggregate query, because it "understands" that you want to count all of the rows where username='USERNAME'. The second query isn't clear enough - you're trying to perform an aggregate function on rows selected by the query, but you also want the rows of the query. My query makes it clear that you only expect one username returned out of the set, so aggregation is not a problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With