【发布时间】:2017-04-28 16:25:52
【问题描述】:
以下是我的表格中的示例
F_ID R_ID DATE Col_A Col_B Col_C Day_Rec Update_No
12 158 20161008 01 99 99 1 -
12 158 20161012 01 01 99 1 -
12 158 20161019 01 02 10 1 -
12 158 20161019 99 01 10 2 1
12 158 20161019 99 02 10 2 2
12 160 20161006 01 99 01 1 -
12 160 20161006 01 99 02 2 1
12 160 20161011 99 01 99 1 -
17 167 20161013 99 01 01 1 -
17 167 20161016 99 02 99 1 -
17 167 20161020 02 01 10 1 -
17 174 20161010 99 01 01 1 -
17 174 20161012 01 02 11 1 -
17 174 20161017 99 02 10 1 -
17 174 20161017 99 96 10 2 1
17 174 20161017 99 07 10 2 2
17 174 20161017 99 99 10 2 3
对于每个 F_ID,R_ID:
When Col_A or Col_B = '01' and Col_C <>'10' - **It is an entry record**
When Col_C = '10' - **It is an exit record**
Day_Rec = 1 means it is the first entry on that date
Day_Rec = 2 means it is the modification on the same date
Update_No - Incremental modification tracker
Update_No is missing when Day_Rec = 1 because its not a modification
Update_No = 1 or 2 or 3..... when Day_Rec = 2 modified multiple times on same date
同一日期可以有多个修改,但 Day_Rec 仍然只有 2 个。多个更新由 Update_No 跟踪
这里的逻辑是
1. Select the earliest valid entry record.
If there is a modification on that earliest date select the entry record with Day_Rec = 2 and highest Update_No
**and**
2. Select the latest exit record.
If there is a modification on that latest date select the exit record with Day_Rec = 2 and highest Update_No for each F_ID, R_ID
我想选择这样我就可以为每个 F_ID 和 R_ID 获得一个有效的进入记录和一个有效的退出记录。仅当每个 F_ID 和 R_ID 都有有效的进入和退出记录时
F_ID R_ID DATE Col_A Col_B Col_C Day_Rec Update_No
12 158 20161008 01 99 99 1 - -> Entry record
12 158 20161019 99 02 10 2 2 -> Exit Record
17 167 20161013 99 01 01 1 - -> Entry record
17 167 20161020 02 01 10 1 - -> Exit Record
17 174 20161010 99 01 01 1 - -> Entry record
17 174 20161017 99 99 10 2 3 -> Exit Record
我有以下查询来按日期获取排序入口和出口记录
SELECT * FROM (
SELECT your_table.*, ROW_NUMBER() OVER(PARTITION BY F_ID, R_ID order by DATE) as rn, 'Entry record' as rec FROM your_table WHERE (Col_A = '01' or Col_B = '01') and Col_C <> '10'
union all
SELECT your_table.*, ROW_NUMBER() OVER(PARTITION BY F_ID, R_ID order by DATE DESC) as rn, 'Exit record' as rec FROM your_table WHERE Col_C = '10'
) t3
where rn = 1
ORDER BY F_ID, R_ID, DATE
但我如何考虑“Day_Rec”或“Update_No”字段。此外,它不会让我知道“仅当每个 F_ID 和 R_ID 都存在进入和退出记录时”条件
更新:
我在选择查询的开头尝试了“Case when”,但没有运气。如果 Day_Rec = 2 在进入和退出记录的 where 条件内,我似乎无法获得如何选择 max(Update_No)
【问题讨论】: