Sql 按嵌套选择分组时列名无效
我有以下代码,当我按原样运行时,它将返回我想要的数据:Sql 按嵌套选择分组时列名无效,sql,tsql,Sql,Tsql,我有以下代码,当我按原样运行时,它将返回我想要的数据: 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 lo
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'
当我想将GROUPBY添加到其中一列时,问题就出现了。然后它抛出一个错误,表示该列不存在,但当我运行查询时,没有按列和名称显示的组
以下是我遇到问题的分组代码:
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的了解不够,无法确定如何修复它。在
select…
部分声明的别名不能在分组方式中使用。您必须在分组方式中复制嵌套的select,或者使用类似的方法:
select Count(q.tagid), q.CostCenter
from
(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'
) q
group by q.CostCenter
在select…
部分中声明的别名不能在分组依据中使用。您必须在分组方式中复制嵌套的select,或者使用类似的方法:
select Count(q.tagid), q.CostCenter
from
(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'
) q
group by q.CostCenter
您可以在此处使用T-SQLAPPLY
功能,它与您使用的相关子查询类似,但可以多次引用,而无需重复相同的代码,如果需要,还可以返回多个列/行
SELECT COUNT(tagid),
CostCenter
FROM item as c
OUTER APPLY
( SELECT TOP 1 Locations.CostCenter
FROM item
INNER JOIN transactions
ON transactions.itemid = item.id
INNER JOIN recvlocationmapping
ON recvlocationid = transactions.locationid
INNER JOIN locations
ON locations.id = servicelocationid
WHERE tagid = c.TagID
AND costcenter != ''
ORDER BY transdate DESC
) AS CostCenter
WHERE createddate BETWEEN '07-01-2012' AND '07-05-2012'
GROUP BY CostCenter
我还对您的子查询做了一些修改,我将位置上的左连接
更改为内部连接
,因为您有,而且成本中心不为空
,因此不需要左连接
,而内部连接
的性能会更好。其次,和成本中心不为空
在和成本中心之后是多余的!=''代码>因为NULL!=''代码>
编辑
经过进一步思考,我认为您可以完全删除其中的相关子查询,并使用连接获得相同的结果。这将提高执行效率:
;WITH CostCenter AS
( SELECT TagID, CostCenter, ROW_NUMBER() OVER(PARTITION BY TagID ORDER BY TransDate DESC) AS RowNumber
FROM item
INNER JOIN transactions
ON transactions.itemid = item.id
INNER JOIN recvlocationmapping
ON recvlocationid = transactions.locationid
INNER JOIN locations
ON locations.id = servicelocationid
WHERE costcenter != ''
)
SELECT COUNT(TagID), CostCenter
FROM Item
INNER JOIN CostCenter
ON Item.TagID = CostCenter.TagID
AND RowNumber = 1
WHERE createddate BETWEEN '07-01-2012' AND '07-05-2012'
GROUP BY CostCenter
您可以在此处使用T-SQLAPPLY
功能,它与您使用的相关子查询类似,但可以多次引用,而无需重复相同的代码,如果需要,还可以返回多个列/行
SELECT COUNT(tagid),
CostCenter
FROM item as c
OUTER APPLY
( SELECT TOP 1 Locations.CostCenter
FROM item
INNER JOIN transactions
ON transactions.itemid = item.id
INNER JOIN recvlocationmapping
ON recvlocationid = transactions.locationid
INNER JOIN locations
ON locations.id = servicelocationid
WHERE tagid = c.TagID
AND costcenter != ''
ORDER BY transdate DESC
) AS CostCenter
WHERE createddate BETWEEN '07-01-2012' AND '07-05-2012'
GROUP BY CostCenter
我还对您的子查询做了一些修改,我将位置上的左连接
更改为内部连接
,因为您有,而且成本中心不为空
,因此不需要左连接
,而内部连接
的性能会更好。其次,和成本中心不为空
在和成本中心之后是多余的!=''代码>因为NULL!=''代码>
编辑
经过进一步思考,我认为您可以完全删除其中的相关子查询,并使用连接获得相同的结果。这将提高执行效率:
;WITH CostCenter AS
( SELECT TagID, CostCenter, ROW_NUMBER() OVER(PARTITION BY TagID ORDER BY TransDate DESC) AS RowNumber
FROM item
INNER JOIN transactions
ON transactions.itemid = item.id
INNER JOIN recvlocationmapping
ON recvlocationid = transactions.locationid
INNER JOIN locations
ON locations.id = servicelocationid
WHERE costcenter != ''
)
SELECT COUNT(TagID), CostCenter
FROM Item
INNER JOIN CostCenter
ON Item.TagID = CostCenter.TagID
AND RowNumber = 1
WHERE createddate BETWEEN '07-01-2012' AND '07-05-2012'
GROUP BY CostCenter
您可能需要将整个子句放入组中;我认为它不能使用别名。您可能需要将整个子句放到GROUP BY中;我认为它不能用别名。