如何从select语句中创建的列进行分组?

时间:2015-02-16 01:30:21

标签: sql sql-server

如何在select语句中按派生列进行分组?

我有一个问题:

select sum(hours) as summedhours, vendoremployeeid, vendorUserName, lineofbusiness, timesheetdate, 
case 
when datepart(weekday,timesheetdate) =1 then dateadd(weekday,4,timesheetdate)
when datepart(weekday,timesheetdate) =2 then dateadd(weekday,3,timesheetdate)
when datepart(weekday,timesheetdate) =3 then dateadd(weekday,2,timesheetdate)
when datepart(weekday,timesheetdate) =4 then dateadd(weekday,1,timesheetdate)
when datepart(weekday,timesheetdate) =5 then dateadd(weekday,0,timesheetdate)
when datepart(weekday,timesheetdate) =6 then dateadd(weekday,6,timesheetdate)
when datepart(weekday,timesheetdate) =7 then dateadd(weekday,5,timesheetdate)
end 
as FridaysGrouping
from viewProjects
group by FridaysGrouping, vendoremployeeid, vendorusername, lineofbusiness, timesheetdate
order by FridaysGrouping asc

当我运行上面的命令时,它给出了一个错误,指出列名“FridaysGrouping”不存在。我相信这是因为sql server的执行顺序是: 来自>在哪里>分组>有>选择>

订购

上面派生列分组的最佳方法是什么?

2 个答案:

答案 0 :(得分:3)

group by

中保留相同的计算列
select sum(hours) as summedhours, vendoremployeeid, vendorUserName, lineofbusiness, timesheetdate, 
case 
when datepart(weekday,timesheetdate) =1 then dateadd(weekday,4,timesheetdate)
when datepart(weekday,timesheetdate) =2 then dateadd(weekday,3,timesheetdate)
when datepart(weekday,timesheetdate) =3 then dateadd(weekday,2,timesheetdate)
when datepart(weekday,timesheetdate) =4 then dateadd(weekday,1,timesheetdate)
when datepart(weekday,timesheetdate) =5 then dateadd(weekday,0,timesheetdate)
when datepart(weekday,timesheetdate) =6 then dateadd(weekday,6,timesheetdate)
when datepart(weekday,timesheetdate) =7 then dateadd(weekday,5,timesheetdate)
end 
as FridaysGrouping
from viewProjects
group by FridaysGrouping, vendoremployeeid, vendorusername, lineofbusiness, timesheetdate,
case 
when datepart(weekday,timesheetdate) =1 then dateadd(weekday,4,timesheetdate)
when datepart(weekday,timesheetdate) =2 then dateadd(weekday,3,timesheetdate)
when datepart(weekday,timesheetdate) =3 then dateadd(weekday,2,timesheetdate)
when datepart(weekday,timesheetdate) =4 then dateadd(weekday,1,timesheetdate)
when datepart(weekday,timesheetdate) =5 then dateadd(weekday,0,timesheetdate)
when datepart(weekday,timesheetdate) =6 then dateadd(weekday,6,timesheetdate)
when datepart(weekday,timesheetdate) =7 then dateadd(weekday,5,timesheetdate)
end
order by FridaysGrouping asc

或使用CTE。在CTE中找到计算列,然后在外group by中执行select。哪个更具可读性

with cte as
(
select hours , vendoremployeeid, vendorUserName, lineofbusiness, timesheetdate, 
case 
when datepart(weekday,timesheetdate) =1 then dateadd(weekday,4,timesheetdate)
when datepart(weekday,timesheetdate) =2 then dateadd(weekday,3,timesheetdate)
when datepart(weekday,timesheetdate) =3 then dateadd(weekday,2,timesheetdate)
when datepart(weekday,timesheetdate) =4 then dateadd(weekday,1,timesheetdate)
when datepart(weekday,timesheetdate) =5 then dateadd(weekday,0,timesheetdate)
when datepart(weekday,timesheetdate) =6 then dateadd(weekday,6,timesheetdate)
when datepart(weekday,timesheetdate) =7 then dateadd(weekday,5,timesheetdate)
end 
as FridaysGrouping
from viewProjects
)
SELECT Sum(hours) summedhours,
       vendoremployeeid,
       vendorUserName,
       lineofbusiness,
       timesheetdate,
       FridaysGrouping
FROM   cte
GROUP  BY FridaysGrouping,
          vendoremployeeid,
          vendorusername,
          lineofbusiness,
          timesheetdate
ORDER  BY FridaysGrouping ASC 

答案 1 :(得分:-2)

前几天我遇到了这个问题,我使用Inner Join解决了这个问题。

Select ... , col1, ... From ...
Inner Join
(Select ... as col1, ... From ...  --newly created column here can be used outside as col1 directly.
)tb1
On ...
Where ...
Group by ...
Order by ...