如何添加和比较子查询的结果

时间:2017-12-08 20:05:36

标签: mysql sql sum subquery

我为以前正在处理的更大的报告编写了以下代码。我有三个子查询产生的列:req_hrs,e_hrs,inprog_hrs。我需要将e_hrs和inprog_hrs加在一起,从而查看该数字是否大于或等于req_hrs。如果是这样,我需要返回*或null值。

有人可以向我解释如何将两个子查询结果(e_hrs和inprog_hrs)一起添加,然后将该结果与req_hrs进行比较,从而返回所述*或NVL?代码如下,谢谢:

SELECT
spriden_last_name lname,
spriden_first_name fname,
spriden_mi mi,
spriden_id id,
x.shrdgmr_majr_code_1 majr,
x.shrdgmr_grad_date grad_dt,
x.shrdgmr_degs_code degs,
DECODE(stvdegs_award_status_ind,'A','*',NULL) award_ind,



**DECODE(NVL(m.smbagen_req_credits_overall,0),0,
  DECODE(NVL(sorcmjr_req_hours_ssdf,0),0,
  DECODE(stvdegc_acat_code,'22',32,'23',64,'24',124,'42',42,999),
  sorcmjr_req_hours_ssdf),m.smbagen_req_credits_overall) req_hrs,** 



**TRUNC(shrlgpa_hours_earned,2) AS e_hrs,**

**(SELECT
     NVL(SUM(sfrstcr_credit_hr),0)
 FROM
     sfrstcr
 WHERE
     sfrstcr_term_code = '&inprog_term'
 AND sfrstcr_pidm = x.shrdgmr_pidm
 AND sfrstcr_rsts_code IN ('RE','RW')
 AND NOT EXISTS (
     SELECT
         'Y'
     FROM
         shrtckn,
         shrtckg j
     WHERE
         shrtckn_pidm = sfrstcr_pidm
     AND shrtckn_term_code = sfrstcr_term_code
     AND shrtckn_crn = sfrstcr_crn
     AND j.shrtckg_pidm = shrtckn_pidm
     AND j.shrtckg_term_code = shrtckn_term_code
     AND j.shrtckg_tckn_seq_no = shrtckn_seq_no
     AND j.shrtckg_seq_no = (
         SELECT
             MAX(k.shrtckg_seq_no)
         FROM
             shrtckg k
         WHERE
             k.shrtckg_pidm = shrtckn_pidm
         AND k.shrtckg_term_code = shrtckn_term_code
         AND k.shrtckg_tckn_seq_no = shrtckn_seq_no))) AS inprog_hrs,**

ROUND(shrlgpa_gpa,2) gpa,
DECODE(SIGN(shrlgpa_gpa - 3.90),0,'S',1,'S',
  DECODE(SIGN(shrlgpa_gpa - 3.75),0,'M',1,'M',
  DECODE(SIGN(shrlgpa_gpa - 3.50),0,'C',1,'C',NULL))) latin,
(SELECT
     m.shrasdl_astd_code_dl
 FROM
     shrasdl m
 WHERE
     m.shrasdl_term_code_effective = (
     SELECT
         MAX(n.shrasdl_term_code_effective)
     FROM
         shrasdl n)
 AND m.shrasdl_min_gpa_term = (
     SELECT
         MAX(n.shrasdl_min_gpa_term)
     FROM
         shrasdl n
     WHERE
         n.shrasdl_term_code_effective = m.shrasdl_term_code_effective
     AND shrlgpa_gpa >= n.shrasdl_min_gpa_term)) honors
FROM
shrdgmr x,
stvdegs,
stvdegc,
spriden,
sorcmjr,
smbagen m,
shrlgpa
WHERE
TO_CHAR(x.shrdgmr_grad_date,'MON-YY') IN ('&grad_dt1', NVL('&grad_dt2','XXX-
00'))
AND x.shrdgmr_seq_no = (
SELECT
    MAX(z.shrdgmr_seq_no)
FROM
    shrdgmr z
WHERE
    z.shrdgmr_pidm = x.shrdgmr_pidm
AND z.shrdgmr_majr_code_1 = x.shrdgmr_majr_code_1
AND z.shrdgmr_grad_date IS NOT NULL)
AND stvdegs_code = x.shrdgmr_degs_code
AND stvdegc_code = x.shrdgmr_degc_code
AND spriden_pidm = x.shrdgmr_pidm
AND spriden_change_ind IS NULL
AND sorcmjr_cmjr_rule(+) = x.shrdgmr_cmjr_rule_1_1
AND REPLACE(m.smbagen_area(+),'-CORE','') = x.shrdgmr_majr_code_1
AND m.smbagen_active_ind(+) = 'Y'
AND m.smbagen_term_code_eff(+) <= x.shrdgmr_term_code_grad
AND ((m.smbagen_area IS NULL)
OR   (m.smbagen_area IS NOT NULL
AND   m.smbagen_term_code_eff = (
  SELECT
      MAX(n.smbagen_term_code_eff)
  FROM
      smbagen n
  WHERE
      REPLACE(n.smbagen_area,'-CORE','') = x.shrdgmr_majr_code_1
  AND n.smbagen_active_ind = 'Y'
  AND n.smbagen_term_code_eff <= x.shrdgmr_term_code_grad)))
AND shrlgpa_pidm(+) = x.shrdgmr_pidm
AND shrlgpa_levl_code(+) = x.shrdgmr_levl_code
AND shrlgpa_gpa_type_ind(+) = 'O'
ORDER BY
spriden_last_name,
spriden_first_name,
spriden_mi
;

1 个答案:

答案 0 :(得分:0)

将两个来源的并集总和与所需数量进行比较。

以简化形式:

select somekey, sum(hrs) worked_hrs, sum(req_hrs) required_hrs
from (
     select
        somekey,
        e_hrs hrs
    from e_hrs_table
    where ...
    union all   -- the "all" is important to leave in!
    select
        somekey,
        inprog_hrs
    from inprog_hrs_table
    where ...
    ) x
join req_hrs_table on req_hrs_table.somekey = x.somekey
where ... -- add req_hrs_table conditions here
group by somekey

您可以添加

having sum(hrs) < sum(req_hrs)

如果您只想要那些不符合配额的行。