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 7856263
In Process

The Archive Base Latest Questions

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

maybe someone could shed some light on the statement that I have been battling

  • 0

maybe someone could shed some light on the statement that I have been battling with the entire day 🙂

I have 3 tables

Holiday

holidayID | userID | dateFrom   | dateTo
1         |      1 | 2012-01-01 | 2012-01-01
2         |      1 | 2012-01-15 | 2012-01-20

Status

statusID | holidayID | statusText
1        |         1 | accepted
2        |         2 | declined

UserSettings

id | userID | HolidaysAllowed
1  |      1 | 20

What I am trying to do is get the following result

Result

HolidaysAllowed | HolidaysLeft (Allowed - Taken) | HolidaysTaken (Sum of Holidays)
20              |                             19 |      1

I am able to get all three columns if I dont state a status – here is the closest I got

SELECT 
IFNULL(SUM( IF(  h.dateTo = h.dateFrom, 1, DATEDIFF( h.dateTo, h.dateFrom ) ) ), 0) AS holidaysTaken, 
IFNULL(us.HolidaysAllowed - ( SUM(  IF(  h.DateTo = h.DateFrom, 1, DATEDIFF( h.DateTo, h.dateFrom ) ) ) ), 0) AS holidaysLeftover, 
us.HolidaysAllowed
FROM userSettings us 
LEFT JOIN holiday h 
ON h.userID = 1 
JOIN status s 
ON s.holidayID = h.holidayID AND s.statusID = 1 
WHERE 
us.userID = 1
GROUP BY h.userID;

The main problem is that if there is no holiday with statusID = 2 then result column 1 & 2 are 0 (which is correct) but column 3 (HolidaysAllowed) is NULL (incorrect as it needs to always return the value from the table UserSettings). The above query only returns a correct response when there is at least 1 record (holiday) with the correct status… what am I doing wrong? 🙂

Its just driving me nuts 🙂 Thanks in advance for any help! Much appreciated!

UPDATE

Thanks to everyone who commented… the expected result is as follows
Get all holidays that have a status = 1 (i.e accepted) then calculate the holidays statistics against that of the holidays allowed. I.e.

20 days in total, 1 holiday accepted = 19 days left to take

thanks again

  • 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-02T20:39:01+00:00Added an answer on June 2, 2026 at 8:39 pm
    SELECT u.userID, u.HolidaysAllowed,
    u.holidaysAllowed - 
    COALESCE( 
        (SELECT SUM( DATEDIFF( h1.dateTo, h1.dateFrom) + 1) 
             FROM holiday h1 
             INNER JOIN status s1
             ON s1.holidayID = h1.holidayID
             WHERE h1.userID = u.userID
             AND s1.statusID = 1
         ), 0) AS HolidaysLeft,
    COALESCE( 
        (SELECT SUM( DATEDIFF( h2.dateTo, h2.dateFrom) + 1) 
             FROM holiday h2 
             INNER JOIN status s2
             ON s2.holidayID = h2.holidayID
             WHERE h2.userID = u.userID
             AND s2.statusID = 1
         ), 0) AS HolidaysTaken
    FROM userSettings u
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe I'm having a really bad day, but could someone possibly help me to
I am writing my first FireFox extension and I have some questions. Maybe someone
maybe someone could recommend some good examples concerning thread executions, thread management. Maybe not
I have one scenario here, and maybe someone could enlighten me at this corner
I have a little problem with setuptools/easy_install; maybe someone could give me a hint
Hi if someone could look at my code and maybe see something I am
I thought I'd post here in the hope that maybe someone with MVVM expertise
I'm having trouble groking something in Linq - maybe someone can give me some
Maybe someone could explain the difference between property: in .h file @property(nonatomic,retain) NSString *someString;
If not, maybe someone could write such an article and the view count would

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.