22,209
社区成员
发帖
与我相关
我的任务
分享
select t3.code,
t3.tclose maxTclose,
t3.tdate maxTdate,
convert(varchar(10),t0.fistAttentionTime,112) firstAttentionTime
into #MaxClose
from aaa t3
left join bbb t0 on t0.code = t3.code and
convert(varchar(10),t0.SetUpDate ,120)= convert(varchar(10),getdate(),120) and
t0.IfIssuance = 1 and
t0.type = 2
where t3.tdate >= cast(convert(varchar(10),t0.fistAttentionTime,112) as int) and
not exists(
select 1 from aaa t4
where t3.code = t4.code and
t4.tclose >t3.tclose and
t4.tdate >= convert(varchar(10),t0.fistAttentionTime,112)
)
select t1.code,
t2.maxTclose,
t2.maxTdate,
t1.tclose minTclose,
t1.tdate minTdate,
(t2.maxTclose-t1.tclose)/t1.tclose rate,
t2.firstAttentionTime
from aaa t1
left join #MaxClose t2 on t2.code = t1.code
where t1.tdate >= t2.firstAttentionTime and
t1.tdate <= t2.maxTdate and
not exists(
select 1 from aaa t4
where t1.code = t4.code and
t4.tclose < t1.tclose and
t4.tdate >= t2.firstAttentionTime and
t4.tdate <= t2.maxTdate)
drop table #MaxClose
create table A(id varchar(10), [count] int, time datetime)
insert into A values('01' , 10, '2009-01-01')
insert into A values('01' , 6 , '2009-02-02')
insert into A values('01' , 18, '2009-02-03')
insert into A values('01' , 20, '2009-01-03')
insert into A values('01' , 3 , '2009-02-05')
create table B(id varchar(10) , time datetime)
insert into B values('01' , '2009-02-01')
go
select
b.id ,
[max] = (select max([count]) from a where a.id = b.id and a.time > b.time),
[max_time] = (select time from a where [count] = (select max([count]) from a where a.id = b.id and a.time > b.time)),
[min] = (select min([count]) from a where a.id = b.id and time between b.time and (select time from a where [count] = (select max([count]) from a where a.id = b.id and a.time > b.time))),
[min_time] = (select time from a where [count] = (select min([count]) from a where a.id = b.id and time between b.time and (select time from a where [count] = (select max([count]) from a where a.id = b.id and a.time > b.time))))
from b
drop table A , B
/*
id max max_time min min_time
---------- ----------- ------------------------------------------------------ ----------- ------------------------------------------------------
01 18 2009-02-03 00:00:00.000 6 2009-02-02 00:00:00.000
(所影响的行数为 1 行)
*/