SQL Query help

I have the query that supposed to count number of days with sessions. The rules are :

from 14:00 to 17:59 on the weekDays on september till May,

max 2 days per week. And what I counting is the number of sessions per week . Which is incorrect.

For example, If I have the session on Monday from 14:00 to 16:00 and sessions on Thursday from 13:30 till  15:30, and 15:30 till 17:30

I still can do 1 session from 16:00 till 18:00 on Monday

DECLARE @sessionDate DATE 
SET @sessionDate = '2015-09-02'

 SELECT WeekStart = convert(varchar(10),dateadd(week, datediff(week, 0, SU.sessionStart), 0),105) ,
 CountID = Count(S.sessionKey) , 
 COUNTDYS = COUNT( DISTINCT DATEPART(weekday, SU.sessionStart) ) FROM session S INNER JOIN sessionUnit SU on SU.sessionKey= S.sessionKey 
 INNER JOIN product P on p.productKey = S.productKey 
 SELECT item AS SPLITLOCATIONKEY from DelimitedSplit8K(S.locationKeyList, ',') )as ca INNER JOIN location AS L2 ON CA.SplitLocationKey = L2.LocationKey 
 AND (L2.locationKey in (34,1) OR S.locationKey in (34,1)) WHERE SU.instructorKey = 19128 
 AND convert(DATE,su.sessionStart) >= DateAdd("d",45,getDate()) 
 AND convert(DATE,su.sessionStart) < DateAdd("d",91,getDate()) AND datediff(day, 0, su.sessionStart) % 7 not in (5, 6) 
 AND DatePart(month, su.sessionStart) in (9, 10, 11, 12, 1, 2, 3, 4, 5) 
 and P.productKey=2 
 AND DatePart(hour, su.sessionStart) between 14 and 18 
 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) 
 HAVING COUNT( DISTINCT DATEPART(weekday, SU.sessionStart) ) >= 2 

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

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.

PortletPaulEE Topic AdvisorCommented:
For your business rules, on what day does a "week" start?

Can you supply a sample of raw data? and then, from that data what is the "expected result"?
erikTsomikSystem Architect, CF programmer Author Commented:
The week start on Monday.

Can we just use the hypothetical data

The rules are from 14:00 to 17:59 on weekdays on September till May max 2 days per week.
PortletPaulEE Topic AdvisorCommented:
Can we just use the hypothetical data? Sure, if you can accept a hypothetical answer ...

But if you want a real answer it's up to you to provide the real data (and expected result)

Imagine a person trying to help many folks all who have questions like yours; supplying test data could take up the entire day for that person.

Alternatively imagine a person who has one or two questions a day, and already has tables of relevant data. It does not take them too long to  choose some data for their questions.

You may think I'm asking too much, but you will get faster/better answers if you supply sample data and the expected result, it is a well proven method.

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
Query Syntax

From novice to tech pro — start learning today.