November 15, 2017 at 7:36 am
Can you anyone please tell the SQL query on how to update the duplicate records dynamically.
PRIMARY_KEY | VERSION | ISCURRENT | BOOKING_NUM |
1 | 1 | 0 | ABCD |
2 | 2 | 0 | ABCD |
3 | 3 | 1 | ABCD |
4 | 4 | 1 | ABCD |
I want to keep the latest record ISCURRENT = 1 and rest should all should be ISCURRENT = 0. Below is the table exactly what I want, please
PRIMARY_KEY | VERSION | ISCURRENT | BOOKING_NUM |
1 | 1 | 0 | ABCD |
2 | 2 | 0 | ABCD |
3 | 3 | 0 | ABCD |
4 | 4 | 1 | ABCD |
Thanks,
Samuel
November 15, 2017 at 8:18 am
This is a a bit of guess work, as your table as a PRIMARY_KEY and Version column with the same values. As we only have 1 example, guessing that the partition should on BOOKING_NUM. Perhaps:
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
November 15, 2017 at 9:19 am
Hi Thom,
Thank you so much the query worked like a charm 🙂
I have another issue can you help me with the query, please?
TRANS_PKEY | VERSION | ISCURRENT | BOOKING_NUM |
7883191 | 1 | 0 | ABCD |
7883192 | 2 | 0 | ABCD |
7883193 | 2 | 0 | ABCD |
7883194 | 3 | 0 | ABCD |
7883195 | 4 | 0 | ABCD |
7883196 | 4 | 1 | ABCD |
I want to update the above table Version dynamically as per the below please:
TRANS_PKEY | VERSION | ISCURRENT | BOOKING_NUM |
7883191 | 1 | 0 | ABCD |
7883192 | 2 | 0 | ABCD |
7883193 | 3 | 0 | ABCD |
7883194 | 4 | 0 | ABCD |
7883195 | 5 | 0 | ABCD |
7883196 | 6 | 1 | ABCD |
Many Thanks,
Raj
November 15, 2017 at 9:24 am
The logic is the same as above, but instead set your value to that of your RN. This suggests you don't quite understand what the solution I supplied does; do you?
Have a go yourself and if you get stuck post back with what you tried.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
November 15, 2017 at 10:09 am
Hi Thom,
Below is the query I've tried and it working.
GO
SELECT *FROM YourTable;
WITH CTE AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY BOOKING_NUM ORDER BY TRANS_PKEY ASC) AS RN FROM YourTable)
UPDATE CTE SET VERSION = RN;
SELECT *FROM YourTable;
GO
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply