重複レコードの値がないリストを取得する必要があります。詳細については、照会されたリストを参照してください。
最初のリストは元のテーブルからクエリされ、2 番目のリストは 2 つの関数を追加した中間結果、3 番目のリストは期待される結果です。
3番目のリストの結果を得るのを手伝ってください。助けてくれる人なら誰でも大歓迎です。
ちなみに、難しいのは最終結果にplsh.plsh_idを保持する必要があるため、2番目のリストでdistinctを使用することはできません。current_smc_value と previous_smc_value は継続する必要があります。
元データは以下のようなものです。
select plsh.plsh_id,
plsh.patr_id,
plsh.smc CURRENT_SMC,
plsh.create_date CURRENT_SMC_DATE
from pat_liver_smc_his plsh
where plsh.patr_id = 34461
order by plsh.plsh_id desc;
結果は
PLSH_ID PATR_ID CURRENT_SMC CURRENT_SMC_DATE
1 10020 34461 22 2/19/2013 1:23:58 PM
2 10019 34461 22 2/19/2013 1:22:57 PM
3 10018 34461 27 2/19/2013 1:22:42 PM
4 10017 34461 27 2/19/2013 1:22:42 PM
5 10016 34461 27 2/19/2013 1:22:32 PM
6 10015 34461 27 2/19/2013 1:22:32 PM
7 10014 34461 27 2/19/2013 1:22:20 PM
8 10013 34461 27 2/19/2013 1:22:20 PM
9 10012 34461 27 2/19/2013 1:22:09 PM
10 10011 34461 27 2/19/2013 1:21:50 PM
11 10010 34461 24 2/19/2013 1:21:36 PM
12 10009 34461 22 2/19/2013 1:21:06 PM
13 10008 34461 2/19/2013 1:21:06 PM
要件は、previous_smc、previous_smc_date、および切り捨て日付を取得する必要があるため、以下のクエリ ステートメントと 2 つの関数を作成します。
select plsh.plsh_id,
plsh.patr_id,
tttt_gen_pkg.get_previous_SMC(plsh.patr_id, plsh.plsh_id) PREVIOUS_SMC,
trunc(tttt_gen_pkg.get_previous_SMC_Date(plsh.patr_id, plsh.plsh_id)) PREVIOUS_SMC_DATE,
plsh.smc CURRENT_SMC,
trunc(plsh.create_date) CURRENT_SMC_DATE
from pat_liver_smc_his plsh
where plsh.patr_id = 34461
order by plsh.plsh_id desc;
function get_previous_SMC(p_patr_id varchar2, p_plsh_id varchar2)
return number is
cursor c_smc is
select plsh.smc
from pat_liver_smc_his plsh
where plsh.patr_id = p_patr_id
and plsh.plsh_id < p_plsh_id
order by plsh.plsh_id desc;
v_rst number;
begin
open c_smc;
fetch c_smc
into v_rst;
close c_smc;
return v_rst;
end;
function get_previous_SMC_Date(p_patr_id varchar2, p_plsh_id varchar2)
return date is
cursor c_smc is
select plsh.create_date
from pat_liver_smc_his plsh
where plsh.patr_id = p_patr_id
and plsh.plsh_id < p_plsh_id
order by plsh.plsh_id desc;
v_rst date;
begin
open c_smc;
fetch c_smc
into v_rst;
close c_smc;
return v_rst;
end;
結果は
PLSH_ID PATR_ID PREVIOUS_SMC PREVIOUS_SMC_DATE CURRENT_SMC CURRENT_SMC_DATE
1 10020 34461 22 2/19/2013 22 2/19/2013
2 10019 34461 27 2/19/2013 22 2/19/2013
3 10018 34461 27 2/19/2013 27 2/19/2013
4 10017 34461 27 2/19/2013 27 2/19/2013
5 10016 34461 27 2/19/2013 27 2/19/2013
6 10015 34461 27 2/19/2013 27 2/19/2013
7 10014 34461 27 2/19/2013 27 2/19/2013
8 10013 34461 27 2/19/2013 27 2/19/2013
9 10012 34461 27 2/19/2013 27 2/19/2013
10 10011 34461 24 2/19/2013 27 2/19/2013
11 10010 34461 22 2/19/2013 24 2/19/2013
12 10009 34461 2/19/2013 22 2/19/2013
13 10008 34461 2/19/2013
期待される最終結果は次のようになります。PLSH_ID は最終結果に必要であるため、上記のクエリ ステートメントとは異なりますが機能しません。
PLSH_ID PATR_ID PREVIOUS_SMC PREVIOUS_SMC_DATE CURRENT_SMC CURRENT_SMC_DATE
1 10020 34461 22 2/19/2013 22 2/19/2013
2 10019 34461 27 2/19/2013 22 2/19/2013
3 10018 34461 27 2/19/2013 27 2/19/2013
10 10011 34461 24 2/19/2013 27 2/19/2013
11 10010 34461 22 2/19/2013 24 2/19/2013
12 10009 34461 2/19/2013 22 2/19/2013
13 10008 34461 2/19/2013
Hi Egor,
Thank you very much for your effort. After a little bit modify your code I got below result,
it seems current_smc and current_smc_date order is correct but the previous_smc and previous_smc_date still not correct,
the result is like below:
PLSH_ID PATR_ID PREVIOUS_SMC PREVIOUS_SMC_DATE CURRENT_SMC CURRENT_SMC_DATE
1 10020 34461 22 2/19/2013
2 10019 34461 22 2/19/2013 22 2/19/2013
3 10018 34461 22 2/19/2013 27 2/19/2013
4 10017 34461 27 2/19/2013 27 2/19/2013
5 10010 34461 27 2/19/2013 24 2/19/2013
6 10009 34461 24 2/19/2013 22 2/19/2013
7 10008 34461 22 2/19/2013 2/19/2013
A little bit modify of your code is like below:
select plsh_id,
patr_id,
PREVIOUS_SMC,
PREVIOUS_SMC_DATE,
CURRENT_SMC,
CURRENT_SMC_DATE
from (select plsh_id,
patr_id,
CURRENT_SMC,
CURRENT_SMC_DATE,
PREVIOUS_SMC,
PREVIOUS_SMC_DATE,
case
when decode(CURRENT_SMC,
lag(CURRENT_SMC) over(partition by t1.patr_id
order by t1.plsh_id desc),
1) = 1 and decode(CURRENT_SMC_DATE,
lag(CURRENT_SMC_DATE)
over(partition by t1.patr_id
order by t1.plsh_id desc),
1) = 1 and
decode(PREVIOUS_SMC,
lag(PREVIOUS_SMC)
over(partition by t1.patr_id order by
t1.plsh_id desc),
1) = 1 and
decode(CURRENT_SMC,
lag(CURRENT_SMC) over(partition by t1.patr_id
order by t1.plsh_id desc),
1) = 1 then
1
end as the_same
from (select plsh.plsh_id,
plsh.patr_id,
trunc(plsh.smc) CURRENT_SMC,
trunc(plsh.create_date) CURRENT_SMC_DATE,
lead(trunc(plsh.smc)) over(partition by plsh.patr_id order by plsh.plsh_id) PREVIOUS_SMC,
lead(trunc(plsh.create_date)) over(partition by plsh.patr_id order by plsh.plsh_id) PREVIOUS_SMC_DATE
from pat_liver_smc_his plsh
where plsh.patr_id = 34461) t1)
where the_same is null
order by patr_id, plsh_id desc;
Can you please help me to correct it? Some of you code I don't understand because you are more senior level than me. Thanks again!!!
最後の完全作業版はDazzLから。変更された作業コードは次のとおりです。
select plsh_id,
patr_id,
previous_smc,
previous_smc_date,
current_smc,
current_smc_date
from (select plsh_id,
patr_id,
previous_smc,
previous_smc_date,
current_smc,
current_smc_date,
row_number() over(partition by patr_id, current_smc, previous_smc order by plsh_id desc) rn
from (select plsh_id,
patr_id,
plsh.smc current_smc,
plsh.create_date current_smc_date,
lag(plsh.smc) over(partition by patr_id order by plsh_id) previous_smc,
lag(plsh.create_date) over(partition by patr_id order by plsh_id) previous_smc_date
from pat_liver_smc_his plsh))
where rn = 1 and patr_id = 34461
order by patr_id, plsh_id desc