Wednesday, December 23

Grouping SQL by month, week etc.

Excellent summary from Jeff Smith on how best to group data by time periods.

Quickhint:

If in doubt, the ‘go-to’ technique is to use

GROUP BY dateadd(month, datediff(month, 0, SomeDate),0)



replacing ‘month’ with any of:




  • year

  • quarter

  • month

  • dayofyear

  • day

  • week

  • hour

  • minute

  • second

  • millisecond

  • microsecond

  • nanosecond



Read the article for a great write up on the whys and wherefores and as always your mileage may vary – testing the various techniques with your own data is the only sure way of getting the performance you need.

0 comments:

Post a Comment