Why is my sum function not working properly?

I am trying to do a simple sum on the "Booking_Factor" column(int) and it is not summing at all as if nothing , the values are the same, any idea? Thank You.
SELECT distinct 

      RES.Description,
      Available_Date,
      --LOC.Scheduling_Location_ID,
      --DEPT.Description,
      --Day_Type_ID,
      --Available_Time_Block_ID,
      --Available_Time_Block_TS,
      Start_Time,
      End_Time,
      Usual_Appt_Duration,
      SUM(Booking_Factor)AS Capacity
      
     -- AVAILBLOCK.Activity_Type_ID,
     -- ELIG.Appointment_Category_ID AS Elig_Appointment_Category_ID,
     --INELIG.Appointment_Category_ID AS Inelig_Appointment_Category_ID 
   
FROM PM.Available_Days AVAILDAY WITH (NOLOCK)
      LEFT JOIN PM.Available_Time_Blocks AVAILBLOCK WITH (NOLOCK) ON AVAILBLOCK.Available_Day_ID = AVAILDAY.Available_Day_ID
      --LEFT JOIN PM.Scheduling_Locations LOC WITH (NOLOCK) ON LOC.Scheduling_Location_ID = AVAILDAY.Scheduling_Location_ID
      --LEFT JOIN PM.Scheduling_Departments DEPT ON DEPT.Scheduling_Department_ID = AVAILDAY.Scheduling_Department_ID
      LEFT JOIN PM.Resources RES WITH (NOLOCK) ON RES.Resource_ID = AVAILDAY.Resource_ID
      --LEFT JOIN PM.Activity_Elig_Appt_Categs ELIG WITH (NOLOCK) ON ELIG.Activity_Type_ID = AVAILBLOCK.Activity_Type_ID
      --LEFT JOIN PM.Activity_Inelig_Appt_Categs INELIG WITH (NOLOCK) ON INELIG.Activity_Type_ID = AVAILBLOCK.Activity_Type_ID


WHERE AVAILDAY.Available_Date = '2017-03-09'
AND RES.Description = 'REYNOLDS, EUGENE II'


group by
 RES.Description,
 Available_Date,
 Start_Time,
 End_Time,
 Usual_Appt_Duration
 

Open in new window

ozzy tDeveloper/Health InformaticsAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>and it is not summing at all as if nothing
Define in simple English what you wish to sum Booking_Factor by.  RES.Description?  Available_Date_ID?

Then eyeballeth all of the columns in your GROUP BY clause, especially Start_Time and End_Time.  Are they unique?  If yes, that kinda defeats the purpose of doing the SUM, and you'll have to remove those columns and possibly others to get the correct sum amount.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Is you Available_Date column a date or a datetime data type? If the last one, then you should use a 24h interval to get all records for that day.
Also, you don't need the DISTINCT keyword since you're using GROUP BY:
SELECT  
      RES.Description,
      Available_Date,
      Start_Time,
      End_Time,
      Usual_Appt_Duration,
      SUM(Booking_Factor)AS Capacity     
FROM PM.Available_Days AVAILDAY WITH (NOLOCK)
      LEFT JOIN PM.Available_Time_Blocks AVAILBLOCK WITH (NOLOCK) ON AVAILBLOCK.Available_Day_ID = AVAILDAY.Available_Day_ID
      LEFT JOIN PM.Resources RES WITH (NOLOCK) ON RES.Resource_ID = AVAILDAY.Resource_ID
WHERE AVAILDAY.Available_Date BETWEEN '20170309' AND '20170310'
	AND RES.Description = 'REYNOLDS, EUGENE II'
GROUP BY
 RES.Description,
 Available_Date,
 Start_Time,
 End_Time,
 Usual_Appt_Duration

Open in new window

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
ozzy tDeveloper/Health InformaticsAuthor Commented:
Thank You for clarifying ! Really helped me out.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Vitor MontalvãoMSSQL Senior EngineerCommented:
It's working as you expected?
0
ozzy tDeveloper/Health InformaticsAuthor Commented:
yes! the following is what worked:
SELECT 

      RES.Description,
      Available_Date,
      --LOC.Scheduling_Location_ID,
      --DEPT.Description,
      --Day_Type_ID,
      --Available_Time_Block_ID,
      --Available_Time_Block_TS,
      --Start_Time ,
      --End_Time,
      --Usual_Appt_Duration,
      sum( Booking_Factor)as capacity
     -- AVAILBLOCK.Activity_Type_ID,
     -- ELIG.Appointment_Category_ID AS Elig_Appointment_Category_ID,
     --INELIG.Appointment_Category_ID AS Inelig_Appointment_Category_ID 
   
FROM PM.Available_Days AVAILDAY 
      LEFT JOIN PM.Available_Time_Blocks AVAILBLOCK  ON AVAILBLOCK.Available_Day_ID = AVAILDAY.Available_Day_ID
      LEFT JOIN PM.Scheduling_Locations LOC WITH (NOLOCK) ON LOC.Scheduling_Location_ID = AVAILDAY.Scheduling_Location_ID
      LEFT JOIN PM.Scheduling_Departments DEPT ON DEPT.Scheduling_Department_ID = AVAILDAY.Scheduling_Department_ID
      LEFT JOIN PM.Resources RES  ON RES.Resource_ID = AVAILDAY.Resource_ID
      LEFT JOIN PM.Activity_Elig_Appt_Categs ELIG WITH (NOLOCK) ON ELIG.Activity_Type_ID = AVAILBLOCK.Activity_Type_ID
      LEFT JOIN PM.Activity_Inelig_Appt_Categs INELIG WITH (NOLOCK) ON INELIG.Activity_Type_ID = AVAILBLOCK.Activity_Type_ID


WHERE AVAILDAY.Available_Date = '2017-03-08'
AND RES.Description = 'example MD, Name'


group by
RES.Description,
 Available_Date
        
   

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Good.
Can you close this question?
Cheers
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Recommendation to close this question by accepting the above comment as solution.
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.