27,579
社区成员
发帖
与我相关
我的任务
分享
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([CODE] varchar(1),[APRICE] numeric(2,1),[FDATE] datetime)
insert [tb]
select 'A',1,'20091001' union all
select 'B',5,'20091001' union all
select 'A',1,'20091002' union all
select 'A',2,'20091003' union all
select 'A',2,'20091004' union all
select 'A',2,'20091005' union all
select 'A',1,'20091006' union all
select 'B',8,'20091006' union all
select 'A',1.5,'20091007' union all
select 'A',1,'20091008' union all
select 'A',1,'20091009' union all
select 'B',6,'20091009' union all
select 'A',1,'20091010'
with cte as
(
select Row_Number() OVER(ORDER BY CODE ASC,FDATE ASC) as RN,*
from tb
)
select CODE,APRICE,FDATE
from cte t
where not exists(select 1 from cte where t.RN-RN=1 and APRICE=t.APRICE and CODE=t.CODE)
order by FDATE asc,CODE asc,RN asc
--> 测试数据:@table
declare @table table([CODE] varchar(1),[APRICE] numeric(2,1),[FDATE] varchar(8))
insert @table
select 'A',1,'20091001' union all
select 'A',1,'20091002' union all
select 'A',2,'20091003' union all
select 'A',2,'20091004' union all
select 'A',2,'20091005' union all
select 'A',1,'20091006' union all
select 'A',1.5,'20091007' union all
select 'A',1,'20091008' union all
select 'A',1,'20091009' union all
select 'A',1,'20091010'
select [CODE],[APRICE],min([FDATE]) as [FDATE]
from
(
select
(select count(1) from @table where [FDATE] <= t.[FDATE]) -
(select count(1) from @table where [FDATE]<= t.[FDATE] and [APRICE] = t.[APRICE]) as id,*
from @table t
) h
group by id,[CODE],[APRICE] order by [CODE],[FDATE]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([CODE] varchar(1),[APRICE] numeric(2,1),[FDATE] datetime)
insert [tb]
select 'A',1,'20091001' union all
select 'B',5,'20091001' union all
select 'A',1,'20091002' union all
select 'A',2,'20091003' union all
select 'A',2,'20091004' union all
select 'A',2,'20091005' union all
select 'A',1,'20091006' union all
select 'B',8,'20091006' union all
select 'A',1.5,'20091007' union all
select 'A',1,'20091008' union all
select 'A',1,'20091009' union all
select 'B',6,'20091009' union all
select 'A',1,'20091010'
with cte as
(
select Row_Number() OVER(ORDER BY CODE ASC,FDATE ASC) as RN,*
from tb
)
select CODE,APRICE,FDATE
from cte t
where not exists(select 1 from cte where t.RN-RN=1 and APRICE=t.APRICE and CODE=t.CODE)
order by FDATE asc,CODE asc
--> 测试数据:@table
declare @table table([CODE] varchar(1),[APRICE] numeric(2,1),[FDATE] varchar(8))
insert @table
select 'A',1,'20091001' union all
select 'A',1,'20091002' union all
select 'A',2,'20091003' union all
select 'A',2,'20091004' union all
select 'A',2,'20091005' union all
select 'A',1,'20091006' union all
select 'A',1.5,'20091007' union all
select 'A',1,'20091008' union all
select 'A',1,'20091009' union all
select 'A',1,'20091010'
select r.* from @table r left join @table h
on r.code = h.code
and cast(r.FDATE as datetime) = dateadd(day,1,h.FDATE)
where r.APRICE <> h.APRICE or h.APRICE is null
--结果
------------------------
A 1.0 20091001
A 2.0 20091003
A 1.0 20091006
A 1.5 20091007
A 1.0 20091008
--> 测试数据: @tb
declare @tb table (CODE varchar(1),APRICE numeric(2,1),FDATE datetime)
insert into @tb
select 'A',1,'20091001' union all
select 'A',1,'20091002' union all
select 'A',2,'20091003' union all
select 'A',2,'20091004' union all
select 'A',2,'20091005' union all
select 'A',1,'20091006' union all
select 'A',1.5,'20091007' union all
select 'A',1,'20091008' union all
select 'A',1,'20091009' union all
select 'A',1,'20091010'
select top 1 * from @tb
union all
select b.code,b.aprice,b.fdate
from @tb a,@tb b
where dateadd(day,1,a.fdate)=b.fdate and a.aprice!=b.aprice
CODE APRICE FDATE
---- --------------------------------------- -----------------------
A 1.0 2009-10-01 00:00:00.000
A 2.0 2009-10-03 00:00:00.000
A 1.0 2009-10-06 00:00:00.000
A 1.5 2009-10-07 00:00:00.000
A 1.0 2009-10-08 00:00:00.000
(5 行受影响)
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([CODE] varchar(1),[APRICE] numeric(2,1),[FDATE] datetime)
insert [tb]
select 'A',1,'20091001' union all
select 'A',1,'20091002' union all
select 'A',2,'20091003' union all
select 'A',2,'20091004' union all
select 'A',2,'20091005' union all
select 'A',1,'20091006' union all
select 'A',1.5,'20091007' union all
select 'A',1,'20091008' union all
select 'A',1,'20091009' union all
select 'A',1,'20091010'
---查询---
select *
from tb t
where not exists(select 1 from tb where datediff(dd,fdate,t.fdate)=1 and aprice=t.aprice)
---结果---
CODE APRICE FDATE
---- ------ ------------------------------------------------------
A 1.0 2009-10-01 00:00:00.000
A 2.0 2009-10-03 00:00:00.000
A 1.0 2009-10-06 00:00:00.000
A 1.5 2009-10-07 00:00:00.000
A 1.0 2009-10-08 00:00:00.000
(所影响的行数为 5 行)
上面多了一个),没环境测试。
with
wang as(select row=row_number() over(order by FDATE),* from tb),
wang1 as (select *,flag=case when aprice=(select aprice from wang where row=t.tow+1)
then 1 else 0 end
from wang t)
select CODE, APRICE, FDATE
from wang1
where id=1
with
wang as(select row=row_number() over(order by FDATE),* from tb),
wang1 as (select *,flag=case when aprice=(select aprice from wang where row=t.tow+1)
then 1 else 0 end)
from wang t)
select CODE, APRICE, FDATE
from wang1
where id=1