Avatar of rcowen00
rcowen00
Flag 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
Microsoft SQL Server

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
Brian Crowe

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?
Dung Dinh

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
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61