I am trying to create an SQL query that depends on the parameters submitted from the form. Inside this, I want to try and share the effects of the main WHERE clause with the WHERE clause in the subquery.
Taking pass_count, for example, I want to count the number of lines where total_passfail = "Pass". This number may, however, change due to the influence of the functions of the main WHERE query (in the main WHERE function, I have 8 possible parameters, such as the date range, user, team, department, which form the general data, respectively).
Is there a way to make a general WHERE clause to influence an additional query, instead of adding 8 more WHERE for each subquery. In fact, I just want the WHERE clause of the subquery to act on top of the dataset returned by the main query.
Any help would be greatly appreciated.
$query = $this->db->join('users', 'users.staff_id = quality.staff_id')
->join('user_info', 'user_info.staff_id = users.staff_id')
->join('teams', 'teams.team_id = users.team_id')
->join('departments', 'teams.dept_id = departments.dept_id')
->join('quality_results', 'quality_results.qms_id = quality.qms_id')
->where('quality.published !=', "")
->where('quality.published !=', "0000-00-00 00:00:00")
->select('users.staff_id, user_info.name, user_info.surname, teams.team_name, departments.dept_name, COUNT(quality.qms_id) AS num_assessments, (SELECT (SUM(quality_results.total_result) / COUNT(quality_results.total_result))) AS average_result, (SELECT COUNT(quality_results.total_passfail) FROM quality_results WHERE quality_results.total_passfail = "Pass") AS pass_count');
if (strlen($query_array['task_date_from'])) {
$query->where('quality.task_datetime >=', $query_array['task_date_from']);
}
if (strlen($query_array['task_date_to'])) {
$query->where('quality.task_datetime <=', $query_array['task_date_to']);
}
(+ 4 MORE IF STATEMENTS HERE)
$result = $query->get('quality');
I am also tired of replacing the subquery with the following, but instead I get the error:
SUM(IF(quality_results.total_passfail = "Pass", 1,0)) AS pass_count
Here is my SQL source code:
SELECT `users`.`staff_id`, `user_info`.`name`, `user_info`.`surname`, `teams`.`team_name`, `departments`.`dept_name`, COUNT(quality.qms_id) AS num_assessments, (SELECT (AVG(quality_results.total_result))) AS average_result, (SELECT COUNT(quality_results.total_passfail) FROM quality_results WHERE quality_results.total_passfail = "Pass") AS pass_count FROM (`quality`) JOIN `users` ON `users`.`staff_id` = `quality`.`staff_id` JOIN `user_info` ON `user_info`.`staff_id` = `users`.`staff_id` JOIN `teams` ON `teams`.`team_id` = `users`.`team_id` JOIN `departments` ON `teams`.`dept_id` = `departments`.`dept_id` JOIN `quality_results` ON `quality_results`.`qms_id` = `quality`.`qms_id` JOIN `quality_tasks` ON `quality`.`task_id` = `quality_tasks`.`task_id` WHERE `quality`.`published` != '' AND `quality`.`published` != '0000-00-00 00:00:00' AND `quality`.`task_datetime` >= '2011-04-09' AND `quality`.`task_datetime` <= '2011-05-09' AND `teams`.`dept_id` = '1'