I have a table with duplicate records. The table format is like this
FIRST Day input Table Name-ABC
ani cdate
7076419812 2016-10-12 00:00:00.000
9168919394 2016-10-12 00:00:00.000
6282358407 2016-10-12 00:00:00.000
9168834643 2016-10-12 00:00:00.000
I want to insert into another table which will contains unique mdn with TS.But for the date 2016-10-12 00:00:00.000 i can easily insert into another table which has the same format.
OUTPUT Table Name-- PQR
MDN, TS
7076419812 2016-10-12 00:00:00.000
9168919394 2016-10-12 00:00:00.000
6282358407 2016-10-12 00:00:00.000
9168834643 2016-10-12 00:00:00.000
but for date 2016-10-13 00:00:00.000 I have the same record of mixed date with same MDN and I want to update existing MDN with the new date and remaining MDN should be inserted as new records. For the second day Input table records are like
ani cdate
7076419812 2016-10-13 00:00:00.000
9168919394 2016-10-13 00:00:00.000
6282358233 2016-10-12 00:00:00.000
9168834609 2016-10-12 00:00:00.000
the output should be like this after processing of second-day input table
mdn ts
7076419812 2016-10-13 00:00:00.000
9168919394 2016-10-13 00:00:00.000
6282358407 2016-10-12 00:00:00.000
9168834643 2016-10-12 00:00:00.000
6282358233 2016-10-12 00:00:00.000
9168834609 2016-10-12 00:00:00.000
This is my query:--
merge PQR as lc
using (select ani, calldate from ABC ) as st
on lc.mdn = st.ani
WHEN MATCHED and lc.ts < st.calldate THEN
update set lc.ts = st.calldate
WHEN NOT MATCHED THEN
insert (mdn,ts) values (st.ani, st.calldate);