Am trying to get desired results from sql query by joining multiple tables. Since am having limited knowledge, seeking help from you all.
Am trying to get audit details per manager and employees. I have 3 tables:
1. HR
2. REQUIRED_AUDITS
3. SCORE_ENTRY
Below is the sample data for HR Table:
+----+---------------+------------------+
| id | manager_email | VP |
+----+---------------+------------------+
| 1 | [email protected] | [email protected] |
| 2 | [email protected] | [email protected] |
| 3 | [email protected] | [email protected] |
| 4 | [email protected] | [email protected] |
| 5 | [email protected] | [email protected] |
+----+---------------+------------------+
Below is the sample data for REQUIRED_AUDITS table:
+----+---------------+----------------+-----------------+----------------+
| ID | manager_email | employee_email | audits_required | audit_eligible |
+----+---------------+----------------+-----------------+----------------+
| 1 | [email protected] | [email protected] | 5 | Y |
| 2 | [email protected] | [email protected] | 2 | Y |
| 3 | [email protected] | [email protected] | 7 | Y |
| 4 | [email protected] | [email protected] | 5 | Y |
| 5 | [email protected] | [email protected] | 25 | N |
+----+---------------+----------------+-----------------+----------------+
Below is the sample data for SCORE_ENTRY table:
+----+---------------+----------------+-------+
| ID | manager_email | employee_email | Score |
+----+---------------+----------------+-------+
| 1 | [email protected] | [email protected] | 85.04 |
| 2 | [email protected] | [email protected] | 100 |
| 3 | [email protected] | [email protected] | 80.50 |
+----+---------------+----------------+-------+
So now I want to show number of audits required for each manager and how many completed and also percentage of completion.
+-------------------------------+----------------------------------------------------------------------------+--------------------------------------------+-----------------------+
| Manager(list from "HR" table) | Total Audits Required(Sum of audits required from "REQUIRED_AUDITS table") | Audits Performed(from "SCORE_ENTRY" table) | Percentage Completion |
+-------------------------------+----------------------------------------------------------------------------+--------------------------------------------+-----------------------+
| [email protected] | 10 | 1 | 10% |
| [email protected] | 9 | 2 | 22.22% |
| [email protected] | - | - | - |
| [email protected] | - | - | - |
| [email protected] | - | - | - |
+-------------------------------+----------------------------------------------------------------------------+--------------------------------------------+-----------------------+
Calculations are as per below:
1. To calculate number of audits i.e., from REQUIRED_AUDITS table:
Considering manager [email protected]:
Total audits required: 7+3 because only two employees are eligible
2. Total audits completed:
audits_required (from REQUIRED AUDITS table) - audits completed (from SCORE_ENTRY table)
As said am having limited knowledge on sql and this look really complex for me and hence reaching out in Stack Overflow.
Appreciate any help.
UPDATE To further simplify the process, I wrote discrete queries excluding HR table and need help in combining below queries.
Query 1: I call this as "DENOMINATOR":
SELECT required_audits.manager_email,
SUM(audits_required) AS "TOTAL_AUDITS_REQUIRED"
FROM required_audits
WHERE Upper(required_audits.audit_eligible) = Upper('Y')
GROUP BY required_audits.manager_email
ORDER BY "total_audits_required" DESC
+-------------------------------+-----------------------+
| required_audits.manager_email | TOTAL_AUDITS_REQUIRED |
+-------------------------------+-----------------------+
| [email protected] | 10 |
| [email protected] | 9 |
+-------------------------------+-----------------------+
Query 2: I call this as "NUMERATOR":
SELECT score_entry.manager_email,
Count(id)
FROM score_entry
GROUP BY score_entry.manager_email
+---------------------------+-----------+
| score_entry.manager_email | Count(id) |
+---------------------------+-----------+
| [email protected] | 1 |
| [email protected] | 2 |
+---------------------------+-----------+
In the final or result, all I need is NUMERATOR**/**DENOMINATOR * 100 in percent. Am getting confused in joins and applying conditions in where clause.
+---------------+-----------------------+------------------+-----------------------+
| Manager | Total Audits Required | Audits Performed | Percentage Completion |
+---------------+-----------------------+------------------+-----------------------+
| [email protected] | 10 | 1 | 10% |
| [email protected] | 9 | 2 | 22.22% |
+---------------+-----------------------+------------------+-----------------------+
On result table, only audit eligible numbers should show up. This is another reason where am going wrong.
Thanks in advance.
Thanks,
Richa