1

收到以下错误:

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'
4

4 回答 4

1
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

请使用此更新

问候

阿舒托什·艾莉亚

于 2013-07-25T13:30:07.803 回答
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
于 2013-07-25T13:24:15.717 回答
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.

于 2013-07-25T13:26:35.483 回答
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
于 2013-07-25T13:27:46.637 回答