如果表只包含的值你發現:
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.
我想你最大的子查詢是找到14月14日之後的CRN序列嗎?爲什麼不在子查詢中包含相同的條件? (儘管無論如何,整個事情可以更簡單高效地寫出來;而依靠隱式轉換並不是一個好日子)。我們無法看到您的原始數據以檢查。 –
就是這樣!我需要在where子查詢中包含日期標準。儘管遇到這個問題,我可以使用Google Analytics找到更簡單的解決方案。謝謝! – Wendell
謝謝Alex!我總是對學習更高效和更簡單的方法感興趣。你有沒有時間分享一個建議的改進? – Wendell