Solved

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

Posted on 2015-01-12
3
110 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
  • 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:
ScottPletcher 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now