如何计算列值?
how to count column values?
这是我的查询:
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;
一切正常。输出:
但是当我尝试计算 r2.LAST_EOD_EXECUTION
的列数时,出现错误 "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;
请问有人能帮忙吗?我如何计算这些值?
删除 distinct
关键字并添加 group by
子句:
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;
编辑:
目前还不完全清楚您要实现的目标,但是如果您要获取每组不同 LAST_EOD_EXECUTION
值的数量,则需要在 [=] 中使用 distinct
关键字16=]:
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;
这是我的查询:
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;
一切正常。输出:
但是当我尝试计算 r2.LAST_EOD_EXECUTION
的列数时,出现错误 "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;
请问有人能帮忙吗?我如何计算这些值?
删除 distinct
关键字并添加 group by
子句:
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;
编辑:
目前还不完全清楚您要实现的目标,但是如果您要获取每组不同 LAST_EOD_EXECUTION
值的数量,则需要在 [=] 中使用 distinct
关键字16=]:
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;