我有以下输出:
ApplicationNumber ApplicationStatusCode CreatedOn AppStat_Disbursed/Unc
1 AnnuledCL 1.3.2019 8:44 Uncompleted
2 AnnuledCL 1.3.2019 8:45 Uncompleted
3 Paid 1.3.2019 8:54 Disbursement
3 Paid 1.3.2019 8:54 Uncompleted
4 Paid 1.3.2019 8:59 Disbursement
4 Paid 1.3.2019 8:59 Uncompleted
4 Paid 1.3.2019 8:59 Uncompleted
5 Paid 1.3.2019 9:03 Uncompleted
6 RejectedAS 1.3.2019 9:04 Uncompleted
我要达到的目的是在以下情况下删除重复的application_numbers。如果一个ApplicationNumber有多个条目(即重复的条目),则应选择“付款”状态而不是“未完成”的行并对其进行优先级排序。但是,如果该applicationNumber没有“付款”状态,则它应从“未完成”状态中移出任何一行。
这是理想的结果:
ApplicationNumber ApplicationStatusCode CreatedOn AppStat_Disbursed/Unc
1 AnnuledCL 1.3.2019 8:44 Uncompleted
2 AnnuledCL 1.3.2019 8:45 Uncompleted
3 Paid 1.3.2019 8:54 Disbursement
4 Paid 1.3.2019 8:59 Disbursement
5 Paid 1.3.2019 9:03 Uncompleted
6 RejectedAS 1.3.2019 9:04 Uncompleted
答案 0 :(得分:0)
一种解决方法是添加由ROW_NUMBER
划分的ApplicationNumber
,然后由AppStat_Disbursed/Unc
进行排序
SELECT ApplicationNumber
,ApplicationStatusCode
,CreatedOn
,[AppStat_Disbursed/Unc]
FROM (
SELECT *
,ROW_NUMBER() OVER (PARTITION BY ApplicationNumber
ORDER BY ApplicationNumber, [AppStat_Disbursed/Unc])
AS rn
FROM YOURTABLE
) A
WHERE rn = 1