0

我有以下代码,当我按原样运行它时,它将返回给我我想要的数据:

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' 

当我想将 group by 添加到其中一列时,问题就来了。然后它会抛出一个错误,说该列不存在,但是当我在没有 Group By 列和名称的情况下运行查询时。

这是我遇到问题的按代码分组:

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 的了解还不够,无法弄清楚如何解决它。

4

2 回答 2

2

部分声明的别名select ...不能用于group by. 您要么必须复制嵌套选择,group by要么使用以下内容:

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
于 2012-07-16T20:27:34.973 回答
1

您可以在APPLY此处使用 T-SQL 功能,它类似于您使用的相关子查询,但可以多次引用而无需重复相同的代码,并且如果需要还可以返回多个列/行。

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

我还对您的子查询进行了一些修改,我将LEFT JOINon 位置更改为 an ,INNER JOIN因为您不需要AND CostCenter IS NOT NULLa并且s 会表现得更好。其次是多余的,因为LEFT JOININNER JOINAND CostCenter IS NOT NULLAND CostCenter != ''NULL != ''

编辑

经过进一步思考,我认为您可以从中完全删除相关的子查询并使用JOINS. 这应该会导致更有效的执行:

;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
于 2012-07-16T21:33:43.303 回答