Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-12
3
Medium Priority
?
128 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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