我需要得到一個沒有重複記錄值的列表。請查看查詢列表以瞭解詳細信息。如何獲取沒有重複記錄的列表?
從原始表中查詢第一個列表,第二個列表是添加兩個函數的中間結果,第三個列表是預期結果。
請幫助獲得第三個列表結果,任何機構可以幫助將非常感激。
順便說一下,困難的部分是需要在最終結果中保留plsh.plsh_id,所以不能在第二個列表中使用不同。 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和TRUNC日期,所以我在下面創建查詢語句和兩種功能。
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!!!
最終完美的作品版本是從DazzaL。修改後的工作代碼如下:
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
什麼重複是你想刪除?您的預期結果也有重複。 – 2013-03-13 15:56:28
我對你的示例輸出有點困惑。你試圖刪除的重複行究竟是什麼?你可以在你的例子中標記它們嗎?第二個至第二個輸出似乎沒有重複的行。 – woemler 2013-03-13 15:57:20
行4,5,6,7,8,9具有相同的PREVIOUS_SMC,PREVIOUS_SMC_DATE CURRENT_SMC和CURRENT_SMC_DATE。最終用戶需要獲取列表以查看一天內smc值的變化。 – 2013-03-13 16:01:06