我通过以下代码收到以下错误:
列'qualitystd.QualityStd_id'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
代码如下:
set dateformat dmy
select qualitystd.qualitystd_id, qualitystd.qualitystd, count(edrec.qualitystd_id) as n, targetreacheddate, treelocation1
from qualitystd
join (
select r.edithistory_id, r.targetreacheddate, qualitystd_id, r.treelocation1
from reports r
right join(
select edithistory_id, QualityStd_ID, MAX(edit_date) AS edit_date
from edit_history
where (edit_date < dateadd(day, 1, '11/21/2014'))
group by edithistory_id,qualitystd_id
) res
on r.edithistory_id = res.edithistory_id
where r.edithistory_id = res.edithistory_id and (targetreacheddate IS NULL) and treelocation1 = 'Illawarra Shoalhaven Local Health District'
) edrec
on edrec.qualitystd_id = qualitystd.qualitystd_id
group by qualitystd.qualitystd_id
order by qualitystd.qualitystd_id
谁能告诉我哪里出错了?前两个选择(由内而外)工作正常。这只是造成问题的最后一个。我以为我已将qualitystd.qualitystd_id字段放在group by语句中。
当我运行以下代码时,它可以正常工作。
select qualitystd.qualitystd_id, qualitystd.qualitystd, targetreacheddate, treelocation1
from qualitystd
join (
select r.edithistory_id, r.targetreacheddate, qualitystd_id, r.treelocation1
from reports r
right join(
select edithistory_id, QualityStd_ID, MAX(edit_date) AS edit_date
from edit_history
where (edit_date < dateadd(day, 1, '11/21/2014'))
group by edithistory_id,qualitystd_id
) res
on r.edithistory_id = res.edithistory_id
where r.edithistory_id = res.edithistory_id and (targetreacheddate IS NULL) and treelocation1 = 'Illawarra Shoalhaven Local Health District'
) edrec
on edrec.qualitystd_id = qualitystd.qualitystd_id
但是,我需要添加count(qualitystd.qualitystd)或count(qualitystd.qualitystd_id),但我似乎无法让它工作。
请帮助......
答案 0 :(得分:0)
您需要从Select语句中删除qualitystd.qualitystd,您只能在select by语句中使用group by子句中使用的那些字段,因此如果您需要计算不同qualitystd.qualitystd_id,则只能使用qualitystd.qualitystd_id当您按照qualitystd.qualitystd_id上的数据进行分组时,计数(qualitystd.qualitystd_id)。所以sql语句将是这样的 `
select qualitystd.qualitystd_id, count(edrec.qualitystd_id) as n, targetreacheddate, treelocation1
from qualitystd
join (
select r.edithistory_id, r.targetreacheddate, qualitystd_id, r.treelocation1
from reports r
right join(
select edithistory_id, QualityStd_ID, MAX(edit_date) AS edit_date
from edit_history
where (edit_date < dateadd(day, 1, '11/21/2014'))
group by edithistory_id,qualitystd_id
) res
on r.edithistory_id = res.edithistory_id
where r.edithistory_id = res.edithistory_id and (targetreacheddate IS NULL) and treelocation1 = 'Illawarra Shoalhaven Local Health District'
) edrec
on edrec.qualitystd_id = qualitystd.qualitystd_id
group by qualitystd.qualitystd_id
order by qualitystd.qualitystd_id
`
或者如果你需要不同qualitystd.qualitystd的计数,sql语句将是这样的
select qualitystd.qualitystd, count(edrec.qualitystd_id) as n, targetreacheddate, treelocation1
from qualitystd
join (
select r.edithistory_id, r.targetreacheddate, qualitystd_id, r.treelocation1
from reports r
right join(
select edithistory_id, QualityStd_ID, MAX(edit_date) AS edit_date
from edit_history
where (edit_date < dateadd(day, 1, '11/21/2014'))
group by edithistory_id,qualitystd_id
) res
on r.edithistory_id = res.edithistory_id
where r.edithistory_id = res.edithistory_id and (targetreacheddate IS NULL) and treelocation1 = 'Illawarra Shoalhaven Local Health District'
) edrec
on edrec.qualitystd_id = qualitystd.qualitystd_id
group by qualitystd.qualitystd
order by qualitystd.qualitystd
希望这可能有助于解决您的问题