2017-06-14 74 views
1

當我運行此查詢WHERE子句子查詢不返回所有行

select SFRSTCA_CRN  CRN, 
    SFRSTCA_BILL_HR Cr_Hr, 
    SFRSTCA_Seq_Number Seq_no  
    from chelink.tmp_stca_201470 
    where SFRSTCA_PIDM = 9573001 
    and SFRSTCA_RSTS_DAte <= '15-oct-14' 

我得到下面的結果

CRN  CR_HR  SEQ_NO 
----- ---------- ---------- 
74705   1   30 
74705   1   37 
74707   1   9 
74707   1   15 
75093   3   4 
75093   3   14 
75093   3   21 
75627   3   5 
75627   3   13 
75627   3   22 
75627   0   33 
77320   3   6 
77320   3   12 
77320   3   23 
77320   3   35 
77776   3   10 
77776   3   11 
78615   3   31 
78615   3   36 

當我試圖通過使用查找每個CRN最大Seq_No在where子句中使用此查詢子查詢

SELECT CRN, 
     Cr_Hr, 
     Seq_No  
FROM 
(select SFRSTCA_CRN  CRN, 
     SFRSTCA_PIDM  PIDM, 
     SFRSTCA_BILL_HR Cr_Hr, 
     SFRSTCA_Seq_Number Seq_no  
    from chelink.tmp_stca_201470 
    where SFRSTCA_PIDM = 9573001 
    and SFRSTCA_RSTS_DAte <= '15-oct-14' 
) STCA_List 
WHERE Seq_No = 
     (SELECT Max(SFRSTCA_Seq_Number) 
     FROM chelink.tmp_stca_201470 STCA2 
     WHERE STCA2.SFRSTCA_PIDM = STCA_List.PIDM 
      AND STCA2.SFRSTCA_CRN = STCA_List.CRN 
    ) 

這些是結果。其中一名CRN失蹤(75093)。是什麼賦予了?

CRN  CR_HR  SEQ_NO 
----- ---------- ---------- 
74705   1   37 
74707   1   15 
75627   0   33 
77320   3   35 
77776   3   11 
78615   3   36 
+3

我想你最大的子查詢是找到14月14日之後的CRN序列嗎?爲什麼不在子查詢中包含相同的條件? (儘管無論如何,整個事情可以更簡單高效地寫出來;而依靠隱式轉換並不是一個好日子)。我們無法看到您的原始數據以檢查。 –

+0

就是這樣!我需要在where子查詢中包含日期標準。儘管遇到這個問題,我可以使用Google Analytics找到更簡單的解決方案。謝謝! – Wendell

+0

謝謝Alex!我總是對學習更高效和更簡單的方法感興趣。你有沒有時間分享一個建議的改進? – Wendell

回答

0

如果表只包含的值你發現:

create table tmp_stca_201470 (sfrstca_crn number, sfrstca_bill_hr number, 
    sfrstca_seq_number number , sfrstca_pidm number, sfrstca_rsts_date date); 

insert into tmp_stca_201470 (sfrstca_crn, sfrstca_bill_hr, 
    sfrstca_seq_number, sfrstca_pidm, sfrstca_rsts_date) 
select 74705, 1, 30, 9573001, date '2014-10-15' from dual 
union all select 74705, 1, 37, 9573001, date '2014-10-15' from dual 
union all select 74707, 1, 9, 9573001, date '2014-10-15' from dual 
union all select 74707, 1, 15, 9573001, date '2014-10-15' from dual 
union all select 75093, 3, 4, 9573001, date '2014-10-15' from dual 
union all select 75093, 3, 14, 9573001, date '2014-10-15' from dual 
union all select 75093, 3, 21, 9573001, date '2014-10-15' from dual 
union all select 75627, 3, 5, 9573001, date '2014-10-15' from dual 
union all select 75627, 3, 13, 9573001, date '2014-10-15' from dual 
union all select 75627, 3, 22, 9573001, date '2014-10-15' from dual 
union all select 75627, 0, 33, 9573001, date '2014-10-15' from dual 
union all select 77320, 3, 6, 9573001, date '2014-10-15' from dual 
union all select 77320, 3, 12, 9573001, date '2014-10-15' from dual 
union all select 77320, 3, 23, 9573001, date '2014-10-15' from dual 
union all select 77320, 3, 35, 9573001, date '2014-10-15' from dual 
union all select 77776, 3, 10, 9573001, date '2014-10-15' from dual 
union all select 77776, 3, 11, 9573001, date '2014-10-15' from dual 
union all select 78615, 3, 31, 9573001, date '2014-10-15' from dual 
union all select 78615, 3, 36, 9573001, date '2014-10-15' from dual; 

,那麼你會得到你所期望的結果:

select sfrstca_crn crn, 
    sfrstca_bill_hr cr_hr, 
    sfrstca_seq_number seq_no 
from tmp_stca_201470 
where sfrstca_pidm = 9573001 
and sfrstca_rsts_date <= date '2014-10-15'; 

     CRN  CR_HR  SEQ_NO 
---------- ---------- ---------- 
    74705   1   30 
... 
... same 19 rows you had 

,並從你的第二個查詢:

select crn, cr_hr, seq_no 
from (
    select sfrstca_crn crn, 
    sfrstca_pidm pidm, 
    sfrstca_bill_hr cr_hr, 
    sfrstca_seq_number seq_no 
    from tmp_stca_201470 
    where sfrstca_pidm = 9573001 
    and sfrstca_rsts_date <= date '2014-10-15' 
) stca_list 
where seq_no = (
    select max(sfrstca_seq_number) 
    from tmp_stca_201470 stca2 
    where stca2.sfrstca_pidm = stca_list.pidm 
    and stca2.sfrstca_crn = stca_list.crn 
); 

     CRN  CR_HR  SEQ_NO 
---------- ---------- ---------- 
    74705   1   37 
    74707   1   15 
    75093   3   21 
    75627   0   33 
    77320   3   35 
    77776   3   11 
    78615   3   36 

7 rows selected. 

明顯的問題談到如果你有更高的序列號和使用範圍之外的日期你過濾行上:

insert into tmp_stca_201470 (sfrstca_crn, sfrstca_bill_hr, 
    sfrstca_seq_number, sfrstca_pidm, sfrstca_rsts_date) 
values (75093, 42, 42, 9573001, date '2014-10-16'); 

第一個查詢仍然得到相同的19行,爲新的範圍之外。但第二個讓你居然看到了結果:

select crn, cr_hr, seq_no 
from (
    select sfrstca_crn crn, 
    sfrstca_pidm pidm, 
    sfrstca_bill_hr cr_hr, 
    sfrstca_seq_number seq_no 
    from tmp_stca_201470 
    where sfrstca_pidm = 9573001 
    and sfrstca_rsts_date <= date '2014-10-15' 
) stca_list 
where seq_no = (
    select max(sfrstca_seq_number) 
    from tmp_stca_201470 stca2 
    where stca2.sfrstca_pidm = stca_list.pidm 
    and stca2.sfrstca_crn = stca_list.crn 
); 
     CRN  CR_HR  SEQ_NO 
---------- ---------- ---------- 
    74705   1   37 
    74707   1   15 
    75627   0   33 
    77320   3   35 
    77776   3   11 
    78615   3   36 

6 rows selected. 

如果你跑自己的子查詢,得到相關值,你會看到:

select sfrstca_crn, max(sfrstca_seq_number) 
from tmp_stca_201470 
where sfrstca_pidm = 9573001 
group by sfrstca_crn; 

SFRSTCA_CRN MAX(SFRSTCA_SEQ_NUMBER) 
----------- ----------------------- 
     75627      33 
     77320      35 
     74707      15 
     75093      42 
     78615      36 
     77776      11 
     74705      37 

7 rows selected. 

但發現該序列75093是42,它不在第一個查詢的19個值列表中。

您可以修改子查詢限制在同一日期範圍:

select crn, cr_hr, seq_no 
from (
    select sfrstca_crn crn, 
    sfrstca_pidm pidm, 
    sfrstca_bill_hr cr_hr, 
    sfrstca_seq_number seq_no 
    from tmp_stca_201470 
    where sfrstca_pidm = 9573001 
    and sfrstca_rsts_date <= date '2014-10-15' 
) stca_list 
where seq_no = (
    select max(sfrstca_seq_number) 
    from tmp_stca_201470 stca2 
    where stca2.sfrstca_pidm = stca_list.pidm 
    and stca2.sfrstca_crn = stca_list.crn 
    and stca2.sfrstca_rsts_date <= date '2014-10-15' 
); 

     CRN  CR_HR  SEQ_NO 
---------- ---------- ---------- 
    74705   1   37 
    74707   1   15 
    75093   3   21 
    75627   0   33 
    77320   3   35 
    77776   3   11 
    78615   3   36 

7 rows selected. 

但是你可以用一個只打表一次簡單得多查詢得到同樣的結果,使用the last() function

select sfrstca_crn crn, 
    max(sfrstca_bill_hr) keep (dense_rank last order by sfrstca_seq_number) cr_hr, 
    max(sfrstca_seq_number) seq_no 
from tmp_stca_201470 
where sfrstca_pidm = 9573001 
and sfrstca_rsts_date <= date '2014-10-15' 
group by sfrstca_crn; 

     CRN  CR_HR  SEQ_NO 
---------- ---------- ---------- 
    74705   1   37 
    74707   1   15 
    75093   3   21 
    75627   0   33 
    77320   3   35 
    77776   3   11 
    78615   3   36 

7 rows selected. 
+0

感謝您的解釋和建議的改進。我很感激你花時間去發現我的邏輯中的一個愚蠢的錯誤,我應該更加勤奮地找到自己的錯誤。表中實際上有超過30萬條記錄。我只是拉了一個小箱子在這裏張貼。我從來沒有使用過dense_rank(在Oracle中有很多東西需要學習)。我會玩。再次感謝! – Wendell