在下表中,有特定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 =&#39; 10&#39;最近的日期)和之前的条目(Col_A或Col_B =&#39; 01&#39;日期)的逻辑就在最后的参赛作品之前)
每个F_ID,R_ID我需要2条记录。最后一个条目和一个前一个条目
感谢。
答案 0 :(得分:0)
您可以尝试以下查询 -
SELECT F_ID, R_ID, DATE, MAX(ENTRY), Col_A, Col_B, Col_C, Score_1, Score_2, Score_3
FROM YOUR_TABLE
WHERE Col_C = '10'
GROUP BY DATE
UNION ALL
SELECT F_ID, R_ID, DATE, MAX(ENTRY), Col_A, Col_B, Col_C, Score_1, Score_2, Score_3
FROM YOUR_TABLE
WHERE (Col_A = '01' OR Col_B = '01')
AND Col_C <> 10
GROUP BY DATE
HAVING DATE < MAX(DATE)
虽然我没有尝试过此查询,但我认为这可以帮助您。