2013-04-15 76 views
1

我有一個查詢可以獲取有關用戶的信息。用戶可能沒有,一個或多個電話號碼。我想要做的是將用戶的最新電話號碼(由seq編號確定)與其他基本人口統計數據一起加入到查詢中。我面對的問題是如果我的子查詢返回null,用戶不會顯示在我的查詢中。我如何處理子查詢中的空值,以便這些行仍然返回?使用返回null的子查詢進行查詢

SELECT SPRIDEN.SPRIDEN_ID AS Student_ID , 
     saradap.SARADAP_APST_CODE , 
     saradap.SARADAP_PIDM , 
     spriden.spriden_first_name , 
     spriden.spriden_last_name , 
     saradap.saradap_program_1 , 
     spraddr.SPRADDR_STREET_LINE1 , 
     spraddr.spraddr_city , 
     spraddr.spraddr_zip , 
     spraddr.spraddr_stat_code , 
     saradap.SARADAP_APPL_DATE , 
     'Individual' AS Account_Name , 
     saradap.SARADAP_CAMP_CODE , 
     CONCAT(sprtele_phone_area, sprtele_phone_number) "Phone" , 
     sprtele_tele_code , 
     sorcont_ctyp_code , 
     sorcont_contact_date , 
     sorcont.sorcont_activity_date 
FROM saradap 
     LEFT JOIN spriden ON saradap.saradap_pidm = spriden_pidm 
     LEFT JOIN spraddr ON saradap.saradap_pidm = spraddr.spraddr_pidm 
     LEFT JOIN sprtele ON saradap.saradap_pidm = sprtele.sprtele_pidm 
     LEFT JOIN sorcont ON saradap.saradap_pidm = sorcont.sorcont_pidm 
WHERE spriden.spriden_change_ind IS NULL 
     AND (saradap.SARADAP_CAMP_CODE = 'D' 
       OR saradap.SARADAP_CAMP_CODE = 'JD' 
      ) 
     AND saradap.saradap_appl_date > SYSDATE - 15 
     AND spraddr.spraddr_seqno = (SELECT MAX(spraddr.spraddr_seqno) 
             FROM  spraddr 
             WHERE  saradap.SARADAP_PIDM = spraddr.spraddr_pidm 
            ) 
     AND sprtele_seqno = (SELECT MAX(SPRTELE_SEQNO) 
           FROM  SATURN.SPRTELE 
           WHERE  sprtele_pidm = saradap.saradap_pidm 
          ) 

回答

1

Sebas讓我中途有這裏是完成查詢。我最終不得不使用CTE。作爲一個方面說明,我是一個新的計算器用戶,我如何給予答案以幫助我找到答案?

with 
tel(sprtele_pidm, mx) as (SELECT sprtele_pidm, MAX(SPRTELE_SEQNO) as "mx" 
        FROM  SATURN.SPRTELE where saturn.sprtele.sprtele_tele_code ='CU' 
        GROUP BY sprtele_pidm 
       ) 
SELECT SPRIDEN.SPRIDEN_ID AS Student_ID, 
saradap.SARADAP_APST_CODE, 
saradap.SARADAP_PIDM, 
spriden.spriden_first_name, 
spriden.spriden_last_name, 
saradap.saradap_program_1, 
spraddr.SPRADDR_STREET_LINE1, 
spraddr.spraddr_city, 
spraddr.spraddr_zip, 
spraddr.spraddr_stat_code, 
null as STAVAPDC_DESC, 
saradap.SARADAP_APPL_DATE, 
'Null' AS remove_reason, 
'Individual'   AS Account_Name, 
saradap.SARADAP_CAMP_CODE, 
CONCAT(sprtele_phone_area, 
sprtele_phone_number) "Phone", 
sprtele_tele_code, 
sorcont_ctyp_code, 
sorcont_contact_date, 
sorcont.sorcont_activity_date 
FROM saradap 
left join spriden on saradap.saradap_pidm   = spriden_pidm 
left join spraddr on saradap.saradap_pidm  = spraddr.spraddr_pidm 
left join tel on saradap.saradap_pidm = tel.sprtele_pidm 
left join sprtele ON saradap.saradap_pidm=sprtele.sprtele_pidm and sprtele.sprtele_pidm = tel.sprtele_pidm and sprtele.sprtele_seqno=tel.mx and sprtele.sprtele_tele_code = 'CU' 
left join sorcont ON saradap.saradap_pidm = sorcont.sorcont_pidm 
WHERE 
spriden.spriden_change_ind  IS NULL 
AND (saradap.SARADAP_CAMP_CODE  = 'D' or saradap.SARADAP_CAMP_CODE  = 'JD') 
AND saradap.saradap_appl_date > SYSDATE - 7 
AND spraddr.spraddr_seqno  = 
(SELECT MAX(spraddr.spraddr_seqno) 
FROM spraddr 
WHERE saradap.SARADAP_PIDM = spraddr.spraddr_pidm 
AND spraddr.spraddr_atyp_code = 'CU' 
) 
AND spraddr.spraddr_atyp_code = 'CU' 
1

我會將邏輯移動到from子句中的子查詢中。使用分析功能,以獲得您想要的電話號碼和地址,與子查詢配送一起:

SELECT . . . 
FROM saradap 
     LEFT JOIN spriden 
     ON saradap.saradap_pidm = spriden_pidm 
     LEFT JOIN (select s.*, row_number() over (partition by spraddr_pidm order by spraddr_seqno desc) as seqnum 
        from spraddr 
       ) spraddr 
     ON saradap.saradap_pidm = spraddr.spraddr_pidm and seqnum = 1 
     LEFT JOIN (select s.*, row_number() over (partition by sprtele_pidm order by sprtele_seqno desc) as seqnum 
        from sprtele 
       ) sprtele 
     ON saradap.saradap_pidm = sprtele.sprtele_pidm and seqnum = 1 
     LEFT JOIN sorcont 
     ON saradap.saradap_pidm = sorcont.sorcont_pidm 
WHERE spriden.spriden_change_ind IS NULL 
     AND (saradap.SARADAP_CAMP_CODE = 'D' 
       OR saradap.SARADAP_CAMP_CODE = 'JD' 
      ) 
     AND saradap.saradap_appl_date > SYSDATE - 15 
0

測試你的子查詢NULL並返回一個非常大的數字或0

看到類似的順序回答:SQL Server ORDER BY date and nulls last

(CASE (SELECT MAX(spraddr.spraddr_seqno) 
            FROM  spraddr 
            WHERE  saradap.SARADAP_PIDM = spraddr.spraddr_pidm) 
    IS NULL 
    THEN 1 
    ELSE 0 
    END