How to group by a Calculated Field

前端 未结 4 803
野性不改
野性不改 2020-12-09 02:54

I need to group by a Calculated field ins SQL Server 2005/2008.

I have the following sql:

select dateadd(day, -7, Convert(DateTime, mwspp.DateDue) +          


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 03:13

    There are several ways of doing so - one involves using a CTE:

    with cte as 
    (select m.*, 
            dateadd(day, -7, Convert(DateTime, DateDue) + (7 - datepart(weekday, DateDue))) datecalc
     from manufacturingweekshortagepartpurchasing m)
    select datecalc, sum(QtyRequired)
    from cte
    where buildScheduleSimID = 10109 and partID = 8366
    group by datecalc
    order by datecalc
    

提交回复
热议问题