sql count per weekend

I need to be able count a number of classes per weekend provided a selected by user date.

DECLARE @sessionDate DATE
	                                SET @sessionDate = '2016-01-10 06:01:00'

	        						
		                                    SELECT  WeekStart = convert(varchar(10),dateadd(week, datediff(week, 0, SU.sessionStart), 0),106) ,CountID = Count(distinct S.sessionKey)
		                                        FROM session S
		                                        INNER JOIN sessionUnit SU on SU.sessionKey= S.sessionKey
		                                        INNER JOIN product P on p.productKey = S.productKey
		                                        CROSS APPLY (
		                                         SELECT item AS SPLITLOCATIONKEY from DelimitedSplit8K(S.locationKeyList, ',')
		                                        )as ca
		                                        INNER JOIN location AS L2 ON CA.SplitLocationKey = L2.LocationKey
		                                        AND (L2.locationKey in (183,34)
		                                        OR S.locationKey in (183,34))
		                                    WHERE SU.instructorKey =2644
		                                        AND  convert(DATE,su.sessionStart) >= DateAdd("d",45,getDate()) AND convert(DATE,su.sessionStart) < DateAdd("d",91,getDate())
		                                         and datepart(dw, Su.sessionStart) in (1, 7)
		                                       
		                                        and P.productKey in (2,4)
		                                        AND DatePart(hour, su.sessionStart) between 8 and 11
		                                        and @sessionDate between  DATEADD(WEEK, DATEDIFF(WEEK, 0, SU.sessionStart), 0) and DATEADD(WEEK, DATEDIFF(WEEK, 0,SU.sessionStart)+1, 0)
		                                       
		                                    GROUP BY dateadd(week, datediff(week, 0, SU.sessionStart), 0)

Open in new window

LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Brian CroweDatabase AdministratorCommented:
Typically I would do something like

SUM(CASE WHEN DATEPART(WEEKDAY,  SU.SessionStart) IN (1, 7) THEN 1 ELSE 0 END) AS WeekendCount
PortletPaulEE Topic AdvisorCommented:
DATEPART(WEEKDAY is unfortunately unreliable due to the impact of the DATEFIRST setting

using modulus 7 on a datediff calculation is not affected by datefirst and is always consistent:

count() increments by 1 for any non-null value, so

       COUNT(CASE WHEN datediff(day,0,SU.SessionStart) % 7 IN (5,6) THEN 1 END)

or, using sum()

       SUM(CASE WHEN datediff(day,0,SU.SessionStart) % 7 IN (5,6) THEN 1 ELSE 0 END)

{+edit}
CREATE TABLE AnyTable
    ([AnyDate] datetime)
;
    
INSERT INTO AnyTable
    ([AnyDate])
VALUES
    ('2015-10-01 00:00:00'),
    ('2015-10-02 00:00:00'),
    ('2015-10-03 00:00:00'),
    ('2015-10-04 00:00:00'),
    ('2015-10-05 00:00:00')
;

**Query 1**:

SET DATEFIRST 1

select
     datename(weekday,AnyDate)
   , CASE WHEN datediff(day,0,AnyDate) % 7 IN (5,6) THEN 1 END
   , format(AnyDate,'yyyy-MM-dd') AnyDate
   , @@datefirst                  DateFirstSetting
   , datepart(weekday,AnyDate)    DateFirstAffected
   , datediff(day,0,Anydate) % 7  AlwaysReliable
from AnyTable


**[Results][2]**:
|          |        |    AnyDate | DateFirstSetting | DateFirstAffected | AlwaysReliable |
|----------|--------|------------|------------------|-------------------|----------------|
| Thursday | (null) | 2015-10-01 |                1 |                 4 |              3 |
|   Friday | (null) | 2015-10-02 |                1 |                 5 |              4 |
| Saturday |      1 | 2015-10-03 |                1 |                 6 |              5 |
|   Sunday |      1 | 2015-10-04 |                1 |                 7 |              6 |
|   Monday | (null) | 2015-10-05 |                1 |                 1 |              0 |
**Query 2**:



SET DATEFIRST 7

select
     datename(weekday,AnyDate)
   , CASE WHEN datediff(day,0,AnyDate) % 7 IN (5,6) THEN 1 END
   , format(AnyDate,'yyyy-MM-dd') AnyDate
   , @@datefirst                  DateFirstSetting
   , datepart(weekday,AnyDate)    DateFirstAffected
   , datediff(day,0,Anydate) % 7  AlwaysReliable
from AnyTable


**[Results][3]**:
|          |        |    AnyDate | DateFirstSetting | DateFirstAffected | AlwaysReliable |
|----------|--------|------------|------------------|-------------------|----------------|
| Thursday | (null) | 2015-10-01 |                7 |                 5 |              3 |
|   Friday | (null) | 2015-10-02 |                7 |                 6 |              4 |
| Saturday |      1 | 2015-10-03 |                7 |                 7 |              5 |
|   Sunday |      1 | 2015-10-04 |                7 |                 1 |              6 |
|   Monday | (null) | 2015-10-05 |                7 |                 2 |              0 |

http://sqlfiddle.com/#!6/13c18/4

Open in new window

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
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.