Reputation: 523
Here's my query:
SELECT DISTINCT r1.STATE_OFFICE_CODE,
r1.AREA_OFFICE_CODE,
r1.AREA_OFFICE_NAME,
r2.LAST_EOD_EXECUTION
FROM report_one r1
INNER JOIN report2 r2 ON r1.distributor_code = r2.distributor_code
ORDER BY r1.AREA_OFFICE_CODE;
It's working fine. Output:
But when I try to count the columns of r2.LAST_EOD_EXECUTION
I get error "not a single group by function":
SELECT DISTINCT r1.STATE_OFFICE_CODE,
r1.AREA_OFFICE_CODE,
r1.AREA_OFFICE_NAME,
r2.LAST_EOD_EXECUTION,
count(r2.LAST_EOD_EXECUTION) AS NumberOfDates
FROM report_one r1
INNER JOIN report2 r2 ON r1.distributor_code = r2.distributor_code
ORDER BY r1.AREA_OFFICE_CODE;
Please can anyone help? How can I count the values?
Upvotes: 1
Views: 80
Reputation: 311326
Drop the distinct
keyword and add a group by
clause:
SELECT r1.STATE_OFFICE_CODE,
r1.AREA_OFFICE_CODE,
r1.AREA_OFFICE_NAME,
r2.LAST_EOD_EXECUTION,
count(r2.LAST_EOD_EXECUTION) AS NumberOfDates
FROM report_one r1
INNER JOIN report2 r2 ON r1.distributor_code = r2.distributor_code
GROUP BY r1.STATE_OFFICE_CODE,
r1.AREA_OFFICE_CODE,
r1.AREA_OFFICE_NAME,
r2.LAST_EOD_EXECUTION
ORDER BY r1.AREA_OFFICE_CODE;
EDIT:
It's not completely clear what you're trying to achieve, but if you're trying to get the number of distinct LAST_EOD_EXECUTION
values per group, you'd need to use the distinct
keyword inside the count
:
SELECT r1.STATE_OFFICE_CODE,
r1.AREA_OFFICE_CODE,
r1.AREA_OFFICE_NAME,
r2.LAST_EOD_EXECUTION,
count(distinct r2.LAST_EOD_EXECUTION) AS NumberOfDates
FROM report_one r1
INNER JOIN report2 r2 ON r1.distributor_code = r2.distributor_code
GROUP BY r1.STATE_OFFICE_CODE,
r1.AREA_OFFICE_CODE,
r1.AREA_OFFICE_NAME,
r2.LAST_EOD_EXECUTION
ORDER BY r1.AREA_OFFICE_CODE;
Upvotes: 2