How to group by a Calculated Field

前端 未结 4 797
野性不改
野性不改 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条回答
  • 2020-12-09 03:08

    Sure, just add the same calculation to the GROUP BY clause:

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

    Edit after comment:

    Like all questions regarding the optimiser, the answer is really "it depends", but most likely it will only be performed once - you'll see this in the execution plan as a Compute Scalar operator.

    Based on this Compute Scalar operation, the optimiser will then decide how to perform the actual aggregation.

    The other answers here (CTE/subquery, etc) are all equally valid, but don't really change the logic - ultimately they will be performing similar operations. SQL Server might treat them differently but it's unlikely. However they will help with readability.

    If you're worried about efficiency you can look at a couple of options, e.g. setting up the calculation as a persisted computed column and using this in an index, or adding interim results into a temporary table.

    The only way to really know for sure is to inspect the Execution Plan/IO statistics when running the query on a typical data set and seeing if you're satisfied with the performance; if not, perhaps investigating one of the above options.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-09 03:20

    If you want to GROUP BY the dateadd calculation, then you will either need to place that formula in the GROUP BY clause or you can wrap your query in another SELECT:

    select DateCalc,
      sum(QtyRequired) TotalQty
    from
    (
      select mwspp.DateDue,
        dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 - datepart(weekday, mwspp.DateDue))) DateCalc,
        mwspp.QtyRequired
      from manufacturingweekshortagepartpurchasing mwspp
      where mwspp.buildScheduleSimID = 10109 
        and mwspp.partID = 8366
    ) src
    group by DateCalc
    order by DateCalc
    
    0 讨论(0)
  • 2020-12-09 03:24

    Simply place the calculation in the GROUP BY clause:

    Replace

    group by mwspp.DateDue
    

    To

    group by dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 - datepart(weekday, mwspp.DateDue)))
    
    0 讨论(0)
提交回复
热议问题