Solved

Sql Server 2008R2  Select statement to group by weeks showing Mondays dates

Posted on 2015-01-12
3
121 Views
Last Modified: 2015-01-12
I need a simple select statement to group data by week, showing Mondays Date for the groupings.

i.e. I want to group 7 days of data into the beginning of the week date.

20141222   sum
20141229   sum
20150105   sum
20150112   sum
0
Comment
Question by:BFanguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:BFanguy
ID: 40545175
think i answered my own question:
CASE datepart(weekday, ShiftDate) 
                      WHEN 1 THEN (CONVERT(varchar(8), DATEADD(day, - 6, ShiftDate), 112)) WHEN 2 THEN (CONVERT(varchar(8), ShiftDate, 112)) WHEN 3 THEN (CONVERT(varchar(8), 
                      DATEADD(day, - 1, ShiftDate), 112)) WHEN 4 THEN (CONVERT(varchar(8), DATEADD(day, - 2, ShiftDate), 112)) WHEN 5 THEN (CONVERT(varchar(8), DATEADD(day, - 3, 
                      ShiftDate), 112)) WHEN 6 THEN (CONVERT(varchar(8), DATEADD(day, - 4, ShiftDate), 112)) WHEN 7 THEN (CONVERT(varchar(8), DATEADD(day, - 5, ShiftDate), 112)) 
                      END AS Starting_Weekday

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40545230
Below is a much easier way to get the correct date, including stripping the time (if needed).

You can add a CROSS APPLY to assign an alias to that name you can use through the query:

SELECT
    CONVERT(varchar(8), Monday_Date, 112) AS Monday_Date,
    SUM(...)
FROM dbo.table_name
CROSS APPLY (
    SELECT DATEADD(DAY, DATEDIFF(DAY, 0, ShiftDate), 0) AS ShiftDate_Time_Stripped
) AS assign_alias_names1
CROSS APPLY (
    SELECT DATEADD(DAY, -DATEDIFF(DAY, 0, ShiftDate_Time_Stripped) % 7, ShiftDate_Time_Stripped) AS Monday_Date
) AS assign_alias_names2
GROUP BY Monday_Date
ORDER BY Monday_Date
0
 

Author Closing Comment

by:BFanguy
ID: 40545469
Thank you sir.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL - format decimal in a string 5 56
How can I use this function? 3 35
SQL works but want to get the XML node data separately 11 57
MSSQL Convert Char to Date Time 5 36
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question