Link to home
Create AccountLog in
Avatar of rcowen00
rcowen00Flag for United States of America

asked on

Week Calculation and Grouping

I have a stored procedure that returns the following data.  The problem I am having is the business users want the report to group by week and sort in Descending order.  Their company week is Mon-Sun and I was using ISO_Week to calculate the week, but I found that the week was then Sat-Sun.  I have listed the results with Wk and ISO_week and you can see how the week order would give me funky order.  I am sure it is simple, but I am having trouble wrapping my mind around how to do the sort order.  Thanks!

WK
Location       Dt                      Cust Ct      Week
2                    2016-01-01      122             1
2                    2016-01-02      155             1
2                    2016-01-03      157             2
2                    2016-01-04      179             2

ISO_Week
Location       Dt                      Cust Ct      Week
2                    2016-01-01      122             53
2                    2016-01-02      155             53
2                    2016-01-03      157             53
2                    2016-01-04      179             1
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Have you tried using DATEPART(WEEK, dt) to calculate your Week value?

It would help if you posted your query since I'm having difficulty discerning exactly what your question is.  Is one of those result sets the correct one?  Is your problem with the week calculation or simply ordering the results?
In my opinion, you can create a base table to store DATE information
Date                             CustomizeWeek_Number      ISO_Week_Number
2016-01-01                         1                                                     53                              
2016-01-02                         1                                                     53
2016-01-03                         2                                                     53
2016-01-04                         2                                                     1


And then you can join main table with above table.  This will help us make flexible report. You can satisfy your customers on both ISO Week and others...

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer