Oracle SQL - 用于计算多个表

时间:2018-02-10 18:25:05

标签: sql oracle

我试图通过连接多个表来获得sql查询所需的结果。由于我的知识有限,寻求大家的帮助。

我试图获得每位经理和员工的审核详细信息。我有3张桌子:

1. HR
2. REQUIRED_AUDITS
3. SCORE_ENTRY

以下是 HR 表的示例数据:

+----+---------------+------------------+
| id | manager_email |        VP        |
+----+---------------+------------------+
|  1 | john@com.com  | jake@com.com     |
|  2 | smith@com.com | kathleen@com.com |
|  3 | maria@com.com | james@com.com    |
|  4 | linda@com.com | david@com.com    |
|  5 | jess@com.com  | kim@com.com      |
+----+---------------+------------------+

以下是 REQUIRED_AUDITS 表的示例数据:

+----+---------------+----------------+-----------------+----------------+
| ID | manager_email | employee_email | audits_required | audit_eligible |
+----+---------------+----------------+-----------------+----------------+
|  1 | john@com.com  | brad@com.com   |               5 | Y              |
|  2 | linda@com.com | gloria@com.com |               2 | Y              |
|  3 | linda@com.com | susan@com.com  |               7 | Y              |
|  4 | john@com.com  | carmen@com.com |               5 | Y              |
|  5 | linda@com.com | aaron@com.com  |              25 | N              |
+----+---------------+----------------+-----------------+----------------+

以下是 SCORE_ENTRY 表的示例数据:

+----+---------------+----------------+-------+
| ID | manager_email | employee_email | Score |
+----+---------------+----------------+-------+
|  1 | linda@com.com | gloria@com.com | 85.04 |
|  2 | linda@com.com | susan@com.com  |   100 |
|  3 | john@com.com  | carmen@com.com | 80.50 |
+----+---------------+----------------+-------+

所以现在我想显示每位经理所需的审核次数以及已完成的审核次数和完成百分比。

+-------------------------------+----------------------------------------------------------------------------+--------------------------------------------+-----------------------+
| Manager(list from "HR" table) | Total Audits Required(Sum of audits required from "REQUIRED_AUDITS table") | Audits Performed(from "SCORE_ENTRY" table) | Percentage Completion |
+-------------------------------+----------------------------------------------------------------------------+--------------------------------------------+-----------------------+
| john@com.com                  |                                                                         10 |                                          1 | 10%                   |
| linda@com.com                 |                                                                          9 |                                          2 | 22.22%                |
| smith@com.com                 |                                                                          - |                                          - | -                     |
| maria@com.com                 |                                                                          - |                                          - | -                     |
| jess@com.com                  |                                                                          - |                                          - | -                     |
+-------------------------------+----------------------------------------------------------------------------+--------------------------------------------+-----------------------+

计算如下:
1.计算审计次数,即从 REQUIRED_AUDITS 表中计算:
考虑经理linda@com.com:
需要进行总审计:7 + 3,因为只有两名员工符合资格 2.完成总审计:
audits_required(来自REQUIRED AUDITS表) - 审计已完成(来自SCORE_ENTRY表)

如上所述,我对sql知之甚少,这对我来说看起来非常复杂,因此在Stack Overflow中也很有用。

感谢任何帮助。

更新 为了进一步简化这个过程,我编写了不包括HR表的离散查询,并在下面的查询中需要帮助。

查询1:我称之为“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 |
+-------------------------------+-----------------------+
| john@com.com                  |                    10 |
| linda@com.com                 |                     9 |
+-------------------------------+-----------------------+

查询2:我将其称为“NUMERATOR”

SELECT score_entry.manager_email, 
       Count(id) 
FROM   score_entry 
GROUP  BY score_entry.manager_email 

+---------------------------+-----------+
| score_entry.manager_email | Count(id) |
+---------------------------+-----------+
| john@com.com              |         1 |
| linda@com.com             |         2 |
+---------------------------+-----------+

在决赛或结果中,我所需要的只有NUMERATOR ** / ** DENOMINATOR * 100%。我在连接和在where子句中应用条件时感到困惑。

+---------------+-----------------------+------------------+-----------------------+
|    Manager    | Total Audits Required | Audits Performed | Percentage Completion |
+---------------+-----------------------+------------------+-----------------------+
| john@com.com  |                    10 |                1 | 10%                   |
| linda@com.com |                     9 |                2 | 22.22%                |
+---------------+-----------------------+------------------+-----------------------+

在结果表中,只显示符合条件的审核数字。这是出错的另一个原因。

提前致谢。

谢谢,
Richa

3 个答案:

答案 0 :(得分:1)

鉴于发布的数据,不需要在此查询中使用HR表,因为manager_email也位于required_audits表中。在现实生活中使用像manager_email这样的列作为外国人将是一个非常糟糕的主意。它应该是hr.id列。

尽管如此,我已将其包含在查询中,因为您已请求。

算术相当简单。复杂的是,您需要将left outer join用于HR到REQUIRED_AUDITS,因为并非所有经理都有需要审计的员工,并且还加入SCORE_ENTRY,因为并非所有员工都经过审计。

select hr.manager_email
       , sum(ra.audits_required) as tot_audits_required
       , count(se.score) as audits_performed
       , (count(se.score) / sum(ra.audits_required)) * 100 as pct_complete      
from hr
left outer join ( select * from  REQUIRED_AUDITS 
                  where audit_eligible = 'Y') ra 
    on ra.manager_email = hr.manager_email
left outer join SCORE_ENTRY se 
    on se.employee_email = ra.employee_email
group by hr.manager_email
order by hr.manager_email
/

Oracle LiveSQL上的演示。

  

“在结果表中,只显示符合条件的审核数字。”

要实现此版本的要求,只需将REQUIRED_AUDITS上的left outer join转换为inner join即可。

答案 1 :(得分:1)

您可以使用多个常用的表格表达式,分别计算每个表达式并连接在一起 - 只是为了让您了解正在进行的操作。

SQL Fiddle

<强>查询

WITH aud(manager_email,Total_audits) AS
  (SELECT manager_email,
    SUM (
    CASE
      WHEN audit_eligible = 'Y'
      THEN audits_required
    END )
  FROM REQUIRED_AUDITS
  GROUP BY manager_email
  ),  --Total_audits

  scores(manager_email,Audits_Performed) AS
  (SELECT manager_email,
    COUNT ( ID )
  FROM SCORE_ENTRY s
  GROUP BY manager_email
  )  --Audits_Performed

SELECT h.manager_email manager,
  a.Total_audits,
  s.Audits_Performed,
  100 * s.Audits_Performed / a.Total_audits percentage_complete
FROM HR h
LEFT OUTER JOIN aud a
ON h.manager_email = a.manager_email
LEFT OUTER JOIN scores s
ON h.manager_email = s.manager_email
ORDER BY 2 DESC NULLS LAST 

<强> Results

|       MANAGER | TOTAL_AUDITS | AUDITS_PERFORMED | PERCENTAGE_COMPLETE |
|---------------|--------------|------------------|---------------------|
|  john@com.com |           10 |                1 |                  10 |
| linda@com.com |            9 |                2 |   22.22222222222222 |
| smith@com.com |       (null) |           (null) |              (null) |
|  jess@com.com |       (null) |           (null) |              (null) |
| maria@com.com |       (null) |           (null) |              (null) |

答案 2 :(得分:-1)

从上面的样本评分表中假设我们要查看得分行的数量,而不是这应该有用的分数

select 
RA.manager_email, 
sum(RA.audits_requiered) as ReqQty,
(select count(manager_email) from SCORE_ENTRY as SE where SE.manager_email = RA.manager_email) as Compleated 

from REQUIRED_AUDITS as RA
group by manager_email
where audit_eligable='Y'

得分表我不清楚这是不是你的意思。