Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7832859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:27:06+00:00 2026-06-02T12:27:06+00:00

Basically what I’m trying to do, there are users that have applicable services within

  • 0

Basically what I’m trying to do, there are users that have applicable services within a date range (monthly)

I want to be able to show the User’s available time for services (in hours), as well as how many hours they have scheduled based a table that holds appointment data

So I want it to basically look like

user             service             available         scheduled
-----------------------------------------------------------------
User1            Service1            120               7
User1            Service2            40                0

The Scheduled column is calculated by taking the SUM of the Start and End fields in my Appointment table.

Now my problem is that if there are no appointments scheduled, I’m unsure of how to still show the information that I’m wanting above.

The relationship between the tables are the following:

  • 1 NOA per User
  • Multiple Services per NOA
  • 1 FormType per Service

  • Appointment holds FormTypeID as a FK

  • ClientAppointment holds the AppointmentID and UserID (the same user with the assigned NOA)

SELECT

 --NOA
 noa.noaID, noa.userID as 'ClientUserID', 

 --Service
 service.BillingCode + service.MOD1 + service.MOD2 + service.MOD3 + service.MOD4 AS     BillingCode, service.FormTypeID, service.StartDate, service.EndDate, service.CurrentUnits, service.Adjustment,

 --Scheduler
 scheduler.* 
 FROM LEL_NOA noa
 LEFT JOIN LEL_Service service ON service.noaID = noa.noaID
 LEFT OUTER JOIN 
 (
 SELECT ca.ClientID, SUM(DATEDIFF(second, a.Start, a.[End]) / 3600) as ScheduledUnits        FROM LEL_Scheduler_Appointment a
 LEFT JOIN LEL_Scheduler_ClientAppointment ca ON a.ApptID = ca.ApptID
 WHERE a.Start BETWEEN '1/1/2012' and '1/31/2012'
   AND a.[End] BETWEEN '1/1/2012' and '1/31/2012'
 GROUP BY ca.ClientID

 ) AS scheduler ON noa.UserID = scheduler.ClientID

WHERE noa.UserID = 4
AND service.StartDate BETWEEN '1/1/2012' and '1/31/2012'
AND service.EndDate BETWEEN '1/1/2012' and '1/31/2012'

ORDER BY 'ClientUserID', BillingCode, FormTypeID, StartDate

The above procedure will pull back for the month, 21 availabl, 7 scheduled, but the person with a UserID = 4 has another service for that time period and I would like to show the information with 0 hours scheduled as the next item in my table.


(noaID, ClientUserID, BillingCode, FormTypeID, StartDate, EndDate, CurrentUnits,     Adjustment, ScheduledUnits) VALUES
(203, 25, 'x888', 6, '4/16/2012 4:24:19 PM', '4/16/2012 4:24:19 PM', 0, 0, 5.000000), 
(203, 25, 'x999', 1, '4/1/2012 12:00:00 AM', '4/30/2012 12:00:00 AM', 10, 0, 5.000000)

The problem is that the first entry, there aren’t any appointments scheduled but yet it’s still showing 5 (which the second sum is correct)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:27:14+00:00Added an answer on June 2, 2026 at 12:27 pm

    Try reworking your query to use an inline sub-query. Performance might suffer, but it will be easier to get a correct query.

    SELECT
    --NOA
    noa.noaID, noa.userID as ClientUserID, 
    --Service
    service.BillingCode + service.MOD1 + service.MOD2 + service.MOD3 + service.MOD4 AS BillingCode,
    service.FormTypeID, service.StartDate, service.EndDate, service.CurrentUnits, service.Adjustment,
    --Scheduler
    (
        SELECT COALESCE(SUM(DATEDIFF(second, a.Start, a.[End]) / 3600), 0) as ScheduledUnits
        FROM LEL_Scheduler_Appointment a
            INNER JOIN LEL_Scheduler_ClientAppointment ca ON a.ApptID = ca.ApptID
        WHERE a.Start BETWEEN '1/1/2012' and '1/31/2012'
            AND a.[End] BETWEEN '1/1/2012' and '1/31/2012'
            AND ca.ClientID = noa.UserID
            AND a.ServiceID = FormTypeID
    ) as ScheduledUnits
    FROM LEL_NOA noa
        LEFT JOIN LEL_Service service ON service.noaID = noa.noaID
    WHERE noa.UserID = 4
        AND service.StartDate BETWEEN '1/1/2012' and '1/31/2012'
        AND service.EndDate BETWEEN '1/1/2012' and '1/31/2012'
    ORDER BY ClientUserID, BillingCode, FormTypeID, StartDate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have some variables that I don't want to preinitialize: originalTime = None
Basically I have an IFRAME that contains a given page - I want to
Basically, I have an open pull request that I want to fix and at
basically i have (state, state code) pairs, that are subsets of country [USA] ->
Basically there is a file called 8puzzle.py and I want to import the file
Basically I have an iframe loaded that is accessed from the parent whenever it
Basically, I have a main page (parent page) with a link that opens up
basically I have a function that resizes elements accordingly with jquery triggering the function
Basically I have a static custom queue of objects I want to process. From
Basically I'm trying to make a web coding tutorial site (awesome that i cant

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.