0

I have these three tables in MYSQL Database:

CREATE TABLE `hr_leave_requests` (
  `id` int(11) UNSIGNED NOT NULL,
  `company_id` int(11) DEFAULT NULL,
  `employee_id` bigint(20) UNSIGNED DEFAULT NULL,
  `leave_type_id` int(11) UNSIGNED NOT NULL,
  `leave_status` tinyint(1) DEFAULT 0 COMMENT '0=Draft,1=Pending/Awaiting Approval, 2=Cancelled,3=Rejected,4=Approved',
  `no_of_days` int(10) UNSIGNED DEFAULT NULL,
  `is_active` tinyint(1) DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `hr_leave_types`
--

CREATE TABLE `hr_leave_types` (
  `id` int(11) UNSIGNED NOT NULL,
  `company_id` int(11) DEFAULT NULL,
  `leave_type_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `no_of_days` int(11) DEFAULT NULL,
  `is_active` tinyint(1) DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `hr_leave_type_details`
--

CREATE TABLE `hr_leave_type_details` (
  `id` int(11) NOT NULL,
  `company_id` int(11) DEFAULT NULL,
  `leave_type_id` int(11) NOT NULL,
  `employment_type_id` int(11) NOT NULL,
  `no_of_days` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `hr_leave_requests`
--
ALTER TABLE `hr_leave_requests`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `hr_leave_types`
--
ALTER TABLE `hr_leave_types`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `hr_leave_type_details`
--
ALTER TABLE `hr_leave_type_details`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `hr_leave_type_details_uniq1` (`company_id`,`leave_type_id`,`employment_type_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `hr_leave_requests`
--
ALTER TABLE `hr_leave_requests`
  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `hr_leave_types`
--
ALTER TABLE `hr_leave_types`
  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `hr_leave_type_details`
--
ALTER TABLE `hr_leave_type_details`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
  1. LeaveType: Now I'll like all the leave_type_name in hr_leave_types to appear and be linked to hr_leave_requests by employee_id and leave_type_id.

  2. Authorized: Each employee is entitled to specific no_of_days based on the leave_type_id and employment_type_id. So all no_of_days in hr_leave_type_details must be displayed as related to employment_type_id of the selected employee

  3. Approved: sum of no_of_days in hr_leave_requests for the particular employee where leave_status is 4. If there is no field at all for any of the related on, it should assign zero

  4. Available: Authorized - Approved

Where I run this Query:

SELECT 
    lt.leave_type_name AS LeaveType,
    ltd.no_of_days as Authorized,
    SUM(lr.no_of_days) as Approved,
    SUM(ltd.no_of_days - lr.no_of_days) as Available
FROM hr_leave_types lt
LEFT JOIN hr_leave_requests lr on lr.leave_type_id = lt.id
  AND lr.leave_status = 4
  AND lr.employee_id = 17
  AND lr.company_id = 1
LEFT JOIN hr_leave_type_details ltd ON ltd.leave_type_id = lr.id
WHERE ltd.employment_type_id = 1
GROUP BY
    lt.leave_type_name,
    lt.no_of_days,
    ltd.no_of_days

Because the table hr_leave_requests is having only one field for the employee, I got this result:

query result

But I need someting like this:

expected result

How do I achieve this?

Thank you

Barmar
  • 741,623
  • 53
  • 500
  • 612
mikefolu
  • 1,203
  • 6
  • 24
  • 57
  • Please [use text, not images/links, for text--including tables & ERDs](https://meta.stackoverflow.com/q/285551/3404097). Use images only for what cannot be expressed as text or to augment text. Include a legend/key & explanation with an image. When you get a result that you don't expect, find out what your misunderstanding is.--Isolate the first subexpression that doesn't output what you expect (which includes confirming that others all give what you expect) & its input & output. Stop trying to code your overall goal & explain what you expected instead from the given code & why. [ask] – philipxy May 19 '20 at 19:41
  • LEFT JOIN ON returns INNER JOIN ON rows UNION ALL unmatched left table rows extended by NULLs. Always know what INNER JOIN ON you want as part of an OUTER JOIN ON. After a LEFT JOIN ON, a WHERE or INNER JOIN that requires a right [sic] table column to be not NULL removes any rows with introduced NULLs, ie leaves only INNER JOIN ON rows, ie "turns OUTER JOIN into INNER JOIN". You have that. Before considering posting please google many clear, concise & precise phrasings of your question/problem/goal with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. – philipxy May 19 '20 at 19:44

0 Answers0