0
在下表中顯示了特定R_ID和F_ID的跟蹤。 Col_C = '10'表示循環中的最後一項。 ENTRY ='1'表示新條目,ENTRY ='2'表示在同一日期修改現有條目。 ENTRY =「2」意味着它是最新的條目從指定日期和多個條件之前的日期中選擇記錄
F_ID R_ID DATE ENTRY Col_A Col_B Col_C Score_1 Score_2 Score_3
85 158 20161005 1 99 99 01 0 0 0
85 158 20161010 1 01 99 99 3 2 2
85 158 20161010 2 01 99 99 3 2 2
85 158 20161012 1 01 01 99 3 2 2
85 158 20161012 2 01 01 99 3 2 2
85 158 20161019 1 99 02 99 3 2 2
85 158 20161022 1 99 99 10 3 2 2
85 158 20161022 2 99 99 10 3 1 2
,我需要選擇這樣,我得到記錄
1. The most recent final entry i.e Col_C = '10' and if Col_C = '10' and ENTRY = '1' or '2' for the same DATE then select the one with ENTRY = '2' and 2. records with Col_A or Col_B = '01' and Col_C != '10' where the date just precedes(before) the DATE of final entry. Also if Col_A or Col_B = '01'and if ENTRY = '1' or '2' for the same DATE then select the one with ENTRY = '2'
對於結果我正在尋找類似
F_ID R_ID DATE ENTRY Col_A Col_B Col_C Score_1 Score_2 Score_3 85 158 20161012 2 01 01 99 3 2 2 85 158 20161022 2 99 99 10 3 1 2
我試過以下
select * from tbl T where Col_C = '10' and T.date=(select min (T2.date) from tbl T2 where T2.Col_C = '10' and T2.R_ID = T.R_ID
and T2.F_ID = T.F_ID)
union
select * from
(
select *
from tbl k
where (k.Col_A = '01' or k.Col_B = '01')
and k.Col_A <> k.Col_B
and k.Col_C <> '10'
k.date =
(select min (k2.date) from tbl k2
where (k2.Col_A='01' or k2.Col_B='01') and
k2.Col_A <> k2.Col_B and
k2.Col_C <> '10' and
k2.R_ID = k.R_ID
and k2.F_ID = k.F_ID
)
union
select *
from tbl S
where S.Col_A = '01' and S.Col_B = '01' and S.Col_C <> '10'
and S.date =
(select min (S2.date) from tbl S2
where S2.Col_A='01' and S2.Col_B='01' and S2.Col_C <> '10' and
S2.R_ID = S.R_ID
and S2.F_ID = S.F_ID
)
)
我無法針對最近的最後一個條目(最近的日期爲Col_C ='10')和之前的條目(Col_A或Col_B ='01'爲最後輸入前的日期)的邏輯
我需要每個F_ID,R_ID有2個記錄。最後一項和最後一項
謝謝。
到目前爲止您嘗試過什麼?請張貼您的嘗試;像「如何做到這些」這樣的問題沒有表現出任何努力,這裏通常不受歡迎 – Aleksej