收到以下错误:
Each GROUP BY expression must contain at least one column that is not an outer reference
我希望列出按设施,月份和年份分组的calculatedDrugDistributionHistory
。我只想了解哪些设施已经导入我们的系统。
我有这个问题:
select
f.name,
MONTH(cddh.dateGiven) as 'date_month',
YEAR(cddh.dateGiven) as 'date_year'
from
calculatedDrugDistributionHistory cddh
inner join facilityIndividuals fi on fi.facilityIndividualId = cddh.facilityIndividualId
inner join facilities f on fi.facilityId = f.facilityId
group by
f.name,
'date_month',
'date_year'
order by
f.name,
'date_month',
'date_year'
答案 0 :(得分:0)
你不能在group by子句中使用别名。试试这个:
select
f.name,
MONTH(cddh.dateGiven) as date_month,
YEAR(cddh.dateGiven) as date_year
from calculatedDrugDistributionHistory cddh
inner join facilityIndividuals fi on fi.facilityIndividualId = cddh.facilityIndividualId
inner join facilities f on fi.facilityId = f.facilityId
group by
f.name,
MONTH(cddh.dateGiven),
YEAR(cddh.dateGiven)
order by
f.name,
date_month,
date_year
答案 1 :(得分:0)
两个问题。首先,您使用单引号作为列别名。使用双引号或方括号。其次,您需要在select
子句中包含所有列,而不应将聚合函数包含在group by
中:
select f.name, MONTH(cddh.dateGiven) as date_month, YEAR(cddh.dateGiven) as date_year'
from calculatedDrugDistributionHistory cddh
inner join facilityIndividuals fi on fi.facilityIndividualId = cddh.facilityIndividualId
inner join facilities f on fi.facilityId = f.facilityId
group by f.name, MONTH(cddh.dateGiven), YEAR(cddh.dateGiven)
order by f.name, date_month, date_year;
您可以使用order by
中的列别名,但不能使用group by
中的列别名。
答案 2 :(得分:0)
您不能在GROUP BY
中使用别名,如果可以,则不应使用单引号编写它们:
select
f.name,
MONTH(cddh.dateGiven) as date_month,
YEAR(cddh.dateGiven) as date_year
from
calculatedDrugDistributionHistory cddh
inner join facilityIndividuals fi on fi.facilityIndividualId = cddh.facilityIndividualId
inner join facilities f on fi.facilityId = f.facilityId
group by
f.name,
MONTH(cddh.dateGiven),
YEAR(cddh.dateGiven)
order by
f.name,
date_month,
date_year
答案 3 :(得分:0)
SELECT
name,
date_month,
date_year
FROM
(select F.NAME,MONTH(cddh.dateGiven) as date_month,
YEAR(cddh.dateGiven) as date_year
from
calculatedDrugDistributionHistory cddh
inner join facilityIndividuals fi on fi.facilityIndividualId = cddh.facilityIndividualId
inner join facilities f on fi.facilityId = f.facilityId)TEMP
group by
name,
date_month,
date_year
order by
name,
date_month,
date_year
请使用此更新
问候
Ashutosh Arya