作者rockchangnew (rock)
看板Database
标题Re: [SQL ] 如何删除Table中不同列且不同栏位重覆值?
时间Thu Mar 24 22:14:39 2016
※ 引述《seabok (思念,留在1999)》之铭言:
: 资料库名称:MS SQL
: 资料库版本:2008
: 内容/问题描述:
: 请问各位贤拜:
: 有一Table A如下:
: ID UPDate OldValue NewValue
: 001 2016/3/24 AAA BBB
: 002 2016/3/23 CCC DDD
: 002 2016/3/24 DDD CCC
: 需求是想捞出在日期区间内有异动的资料,也就是最终的OldValue <> NewValue。
: 可是在表中第2、3列,ID=002的资料在3/23先是自CCC改成DDD,
: 然後在3/24又自DDD改回CCC,最终结果其实是没有异动。
: 请问SQL该如何下才能直接排除这种情形呢?(也就是只有ID=001这笔符合)
: 先感谢大家了~
with tb1 as
(select ROW_NUMBER() over(partition by id order by [update]) as num,* from table_1 where [update] between '2016/3/23' and '2016/3/24')
,tb2 as (select ROW_NUMBER() over(partition by id order by [update] desc) as num,* from table_1 where [update] between '2016/3/23' and '2016/3/24')
select * from tb1 a
inner join tb2 b on a.ID=b.ID
where a.num=1 and b.num=1 and a.OldValue<>b.NewValue
--
Sent from my Windows
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.34.194.8
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1458828881.A.DF0.html
1F:推 cutekid: 推(Y) 03/25 13:31