sql >> Database teknologi >  >> RDS >> Oracle

Oracle SQL - Forespørgsel til at beregne værdier fra flere tabeller

Du kan bruge flere almindelige tabeludtryk, beregne hver separat og slå sammen - bare for at du skal forstå, hvad der foregår.

SQL Fiddle

Forespørgsel :

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 
 

Resultater :

| MANAGER | TOTAL_AUDITS | AUDITS_PERFORMED | PERCENTAGE_COMPLETE | |---------------|--------------|------------------|---------------------| | [email protected] | 10 | 1 | 10 | | [email protected] | 9 | 2 | 22.22222222222222 | | [email protected] | (null) | (null) | (null) | | [email protected] | (null) | (null) | (null) | | [email protected] | (null) | (null) | (null) |

  1. Enkel måde at køre sqlplus script fra java

  2. MySQL select distinct virker ikke

  3. postgresql:Hvorfor skal jeg angive -h localhost, når jeg kører psql?

  4. MySQL FEJL 502:VÆLG * FRA tabel WHERE uid=1 ORDER BY id DESC LIMIT 0, 100; Sideinddeling virker ikke