简体   繁体   中英

how to count column values?

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM