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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:44:17+00:00 2026-06-17T16:44:17+00:00

I want to subtract between two rows of different table: I have created a

  • 0

I want to subtract between two rows of different table:

I have created a view called leave_taken and table called leave_balance.

I want this result from both table:

leave_taken.COUNT(*) - leave_balance.balance

and group by leave_type_id_leave_type

Code of both table

—————–View Leave_Taken———–

CREATE ALGORITHM = UNDEFINED DEFINER=`1`@`localhost` SQL SECURITY DEFINER 
VIEW `leave_taken` 
AS 
   select 
     `leave`.`staff_leave_application_staff_id_staff` AS `staff_leave_application_staff_id_staff`,
     `leave`.`leave_type_id_leave_type` AS `leave_type_id_leave_type`,
     count(0) AS `COUNT(*)` 
   from 
   (
      `leave` 
      join `staff` on((`staff`.`id_staff` = `leave`.`staff_leave_application_staff_id_staff`))
   ) 
   where (`leave`.`active` = 1) 
   group by `leave`.`leave_type_id_leave_type`;

—————-Table leave_balance———-

CREATE TABLE IF NOT EXISTS `leave_balance` (
  `id_leave_balance` int(11) NOT NULL AUTO_INCREMENT,
  `staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `balance` int(3) NOT NULL,
  `date_added` date NOT NULL,
  PRIMARY KEY (`id_leave_balance`),
  UNIQUE KEY `id_leave_balance_UNIQUE` (`id_leave_balance`),
  KEY `fk_leave_balance_staff1` (`staff_id_staff`),
  KEY `fk_leave_balance_leave_type1` (`leave_type_id_leave_type`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

——- Table leave ———-

CREATE TABLE IF NOT EXISTS `leave` (
  `id_leave` int(11) NOT NULL AUTO_INCREMENT,
  `staff_leave_application_id_staff_leave_application` int(11) NOT NULL,
  `staff_leave_application_staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `date` date NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `date_updated` date NOT NULL,
  PRIMARY KEY (`id_leave`,`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`),
  KEY `fk_table1_leave_type1` (`leave_type_id_leave_type`),
  KEY `fk_table1_staff_leave_application1` (`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
  • 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-17T16:44:18+00:00Added an answer on June 17, 2026 at 4:44 pm

    Well, I still don’t think you’ve provided enough information. It would be very helpful to have some sample data and your expected output (in tabular format). That said, I may have something you can start working with. This query finds all staff members, calculates their current leave (grouped by type), and determines the difference between that and their balance by leave type. Take a look at it, and more importantly (perhaps) the sqlfiddle here that I used which has the sample data in it (very important to determining if this is the correct path for your data).

    SELECT
        staff.id_staff,
        staff.name,
        COUNT(`leave`.id_leave) AS leave_count,
        leave_balance.balance,
        (COUNT(`leave`.id_leave) - leave_balance.balance) AS leave_difference,
        `leave`.leave_type_id_leave_type AS leave_type
      FROM
        staff
        JOIN `leave` ON staff.id_staff = `leave`.staff_leave_application_staff_id_staff
        JOIN leave_balance ON
          (
            staff.id_staff = leave_balance.staff_id_staff
            AND `leave`.leave_type_id_leave_type = leave_balance.leave_type_id_leave_type
          )
      WHERE 
        `leave`.active = 1
      GROUP BY
        staff.id_staff, leave_type;
    

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to subtract two signed 8bit integers in JavaScript. As JavaScript doesn't have
I want to subtract between two date time values using SQL in MySQL such
I have two different dates and I want to know the difference in days
I want to substract two arrays in Matlab but they are of different sizes.
I have two floats in Python that I'd like to subtract, i.e. v1 =
The following code has been created from a different post, but I now have
i want to subtract 1 from a variable who is stored in my table
I want to calculate the working hours between two dates. So I need saturday/sunday
I want to calculate the date difference between form date and two date..Am using
I want to subtract one date to another using php and display result in

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.