使用条件更新在SQL Server中合并查询

时间:2016-10-14 19:54:53

标签: sql sql-server-2008 sql-merge

我有一张包含重复记录的表格。表格格式如下

第一天输入表名-ABB

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

我想插入到另一个表中,该表将包含TS.But的唯一mdn,日期为2016-10-12 00:00:00.000我可以轻松地插入到具有相同格式的另一个表中。

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

但是对于日期2016-10-13 00:00:00.000我有相同MDN的混合日期记录,我想用新日期更新现有MDN,剩余的MDN应作为新记录插入。     对于第二天,输入表记录类似于

   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

在处理第二天输入表之后输出应该是这样的

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);

这是我的疑问: -

{{1}}

1 个答案:

答案 0 :(得分:0)

这对于评论来说太长了,但是将其发布给其他人。

您的查询似乎很好,您需要详细说明没有正确返回的内容。以下是一些使用相同逻辑的测试数据......

if object_id ('tempdb..#PRQ') is not null drop table #PRQ
create table #PRQ (mdn bigint, ts datetime)

insert into #PRQ (mdn, ts)
values
(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')



if object_id ('tempdb..#ABC') is not null drop table #ABC
create table #ABC (ani bigint, cdate datetime)

--this is simulating "day 2" where the date is changing for 7076419812 and 9168919394

insert into #ABC (ani, cdate)
values
(7076419812,'2016-10-13 00:00:00.000'),  --date change
(9168919394,'2016-10-13 00:00:00.000'),  --date change
(6282358407,'2016-10-12 00:00:00.000'),
(9168834643,'2016-10-12 00:00:00.000')

--run the merge with the same logic you used

MERGE #PRQ AS T
USING(SELECT ani, cdate FROM #ABC) AS S
ON T.mdn = s.ani
WHEN MATCHED AND t.ts < s.cdate THEN
UPDATE SET t.ts = s.cdate
WHEN NOT MATCHED THEN
INSERT (mdn,ts) VALUES (s.ani, s.cdate);

--you will see the dates changed for 7076419812 and 9168919394
SELECT * FROM #PRQ

--Now we will update it for "day 3"

update #ABC
set cdate = '2016-10-14 00:00:00.000'
where ani in (7076419812,9168919394)

--run the same merge and select the results from #PRQ

MERGE #PRQ AS T
USING(SELECT ani, cdate FROM #ABC) AS S
ON T.mdn = s.ani
WHEN MATCHED AND t.ts < s.cdate THEN
UPDATE SET t.ts = s.cdate
WHEN NOT MATCHED THEN
INSERT (mdn,ts) VALUES (s.ani, s.cdate);

--see the date changes for 7076419812 and 9168919394
SELECT * FROM #PRQ

--now add two new records with two new ani to your ABC table

insert into #ABC (ani, cdate)
VALUES
(5469358407,'2016-10-15 00:00:00.000'),
(1234834643,'2016-10-15 00:00:00.000')

--and now run the merge again, and see the added rows since they won't match

MERGE #PRQ AS T
USING(SELECT ani, cdate FROM #ABC) AS S
ON T.mdn = s.ani
WHEN MATCHED AND t.ts < s.cdate THEN
UPDATE SET t.ts = s.cdate
WHEN NOT MATCHED THEN
INSERT (mdn,ts) VALUES (s.ani, s.cdate);

SELECT * FROM #PRQ