我有下面的代碼,當我運行它是它將返回給我的數據,我想:無效列名當集團通過嵌套查詢
select tagid,
(select TOP 1 Locations.CostCenter
from item
inner join transactions on transactions.itemid = item.id
inner join recvlocationmapping on recvlocationid = transactions.locationid
left outer join locations on locations.id = servicelocationid
where tagid = c.TagID
and costcenter != ''
and Costcenter is not null
order by transdate desc) as CostCenter
from item as c where createddate between '07-01-2012' and '07-05-2012'
問題是當我要添加按其中一列分組。然後它會拋出一個錯誤,說該列不存在,但是當我運行沒有該組的列時,該列和名稱存在。
下面是組由我遇到的問題代碼:
select Count(tagid),
(select TOP 1 Locations.CostCenter
from item
inner join transactions on transactions.itemid = item.id
inner join recvlocationmapping on recvlocationid = transactions.locationid
left outer join locations on locations.id = servicelocationid
where tagid = c.TagID
and costcenter != ''
and Costcenter is not null
order by transdate desc) as CostCenter
from item as c where createddate between '07-01-2012' and '07-05-2012'
group by CostCenter
我認爲這是與我如何返回數據的問題,但我不知道足夠的SQL圖瞭解如何解決它。
您可能需要將整個子句放入GROUP BY;我不認爲它可以使用別名。 – 2012-07-16 20:25:36