capturing totals by weeks, using a date for week start

Hi Experts,
I have a query that captures a count on a particular day at a location,  I need to put the report in a format by week(Monday thru Sunday).  

Here is my query
SELECT CAST(sessionstart as date) as Date,
       count(UserName) as count,
       e.SchCode,
       LOCNAME,
       D2lRole
FROM [GSDR].[d2l].[D2L_Reporting_D2LSessionExport] as d
      cross join (SELECT DATEDIFF(week, 0, getdate())*7 lastmonday) a
      inner join [ProdDatamart].gsdr.d2l.GCPS_OUX_Enrollments_School_tmp as e 
       on d.UserName=e.empno
      inner join [ProdDatamart].gsdr.gems.location l 
       on e.schCode=l.loc
where SessionStart >= a.lastmonday - 7
       and SessionStart < a.lastmonday 
       and D2LRole = 'Student'
       and activesessionduration>0
       and schCode in ('700', '725','855')
group by D2LRole,
             CAST(sessionstart as date) ,
             schCode,
             LOCNAME

order by  
         LOCNAME,
         Date,
         D2LRole

Open in new window


see below for results(I am only including 1 school)
Date          count  SchCode      LOCNAME           D2lRole
8/4/2014       27       700       NORCROSS HIGH      Student
8/5/2014       61       700       NORCROSS HIGH      Student
8/6/2014       258      700       NORCROSS HIGH      Student
8/7/2014       71       700       NORCROSS HIGH      Student
8/8/2014       45       700       NORCROSS HIGH      Student
8/9/2014       32       700       NORCROSS HIGH      Student
8/10/2014      74       700       NORCROSS HIGH      Student
8/11/2014      122      700       NORCROSS HIGH      Student
8/12/2014      92       700       NORCROSS HIGH      Student
8/13/2014      331      700       NORCROSS HIGH      Student
8/14/2014      164      700       NORCROSS HIGH      Student
8/15/2014      357      700       NORCROSS HIGH      Student
8/16/2014      41       700       NORCROSS HIGH      Student
8/17/2014      94       700       NORCROSS HIGH      Student

Open in new window

for the results above I have 14 records but I only what 2 records  

This is what  want

Date                count      SchCode          LOCNAME               D2lRole
8/4/2014        568             700               NORCROSS HIGH       Student
8/11/2014      1201           700               NORCROSS HIGH       Student
Butterfly2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
Try changing this:
CAST(sessionstart as date)

To:
CAST(sessionstart as date) - DATEPART(WEEKDAY, sessionstart) + 2

in both the SELECT and GROUP BY clauses.
0
Butterfly2Author Commented:
I get this error:

Operand type clash: date is incompatible with int
0
Shaun KlineLead Software EngineerCommented:
Must be because of the casting to Date. I used a datetime when I tested.
Use the DATEADD function:

DATEADD(DAY, - DATEPART(WEEKDAY, CAST(sessionstart as date)) + 2, CAST(sessionstart as date))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
You just needt to group on the week rather than day:

SELECT  dateadd(week, datediff(week, 0, sessionstart), 0) as Date,
        count(UserName) as count,
        e.SchCode,
        LOCNAME,
        D2lRole
 FROM [GSDR].[d2l].[D2L_Reporting_D2LSessionExport] as d
       cross join (SELECT DATEDIFF(week, 0, getdate())*7 lastmonday) a
       inner join [ProdDatamart].gsdr.d2l.GCPS_OUX_Enrollments_School_tmp as e 
        on d.UserName=e.empno
       inner join [ProdDatamart].gsdr.gems.location l 
        on e.schCode=l.loc
 where SessionStart >= a.lastmonday - 7
        and SessionStart < a.lastmonday 
        and D2LRole = 'Student'
        and activesessionduration > 0
        and schCode in ('700', '725','855')
 group by D2LRole,
              dateadd(week, datediff(week, 0, sessionstart), 0),
              schCode,
              LOCNAME

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.