I need a single query without using CTE / row_number
to remove duplicates based on the ACTID
in SQL Server.
Table :
ACTID TRID CD DATE
-------------------
1 1 0 12/04 - To be deleted
1 2 2 11/04 - To be deleted
1 3 4 10/04 - Do not delete
2 4 0 10/04 - To be deleted
2 5 2 11/04 - Do not delete
3 6 4 12/04 - Do not delete
3 7 4 11/04 - To be deleted
Conditions:
- Do not delete if CD = 4
- If CD in 0, 2 or 4, 4, keep the newest record
- If CD in 2, 4, delete 2
The output should consist of TRID's that needs to be deleted.
Able to get the required outcome using cte but unable to integrate it in Java jpa so looking for simpler approach without using cte/row_number.
2条答案
按热度按时间hkmswyz61#
Though the query seems messy it will do the trick.
Delete Query:
select * from myTable
fiddle
py49o6xq2#
Here's my try:
It returns list of ids to remove by filtering out: