27,580
社区成员
发帖
与我相关
我的任务
分享
create table #table
(date_1 varchar(10),count_1 int)
insert into #table
select '2014-12-01',1 union all
select '2014-12-02',3 union all
select '2014-12-03',5 union all
select '2014-12-04',3 union all
select '2014-12-05',2 union all
select '2014-12-06',1 union all
select '2015-01-01',5 union all
select '2015-01-02',4 union all
select '2015-01-03',3
select *,
case
when count_1 > (select count_1 from #table where date_1= (select top 1 date_1 from #table where date_1 <a.date_1 order by 1 desc )) then '下降'
when count_1 < (select count_1 from #table where date_1= (select top 1 date_1 from #table where date_1 <a.date_1 order by 1 desc )) then '上升'
when count_1 = (select count_1 from #table where date_1= (select top 1 date_1 from #table where date_1 <a.date_1 order by 1 desc )) then '不变'
else '上升'
end
from #table a
order by date_1
create table wh
(date_1 varchar(10),count_1 int)
insert into wh
select '2014-12-01',1 union all
select '2014-12-02',3 union all
select '2014-12-03',5 union all
select '2014-12-04',3 union all
select '2014-12-05',2 union all
select '2014-12-06',1 union all
select '2015-01-01',5 union all
select '2015-01-02',4 union all
select '2015-01-03',3
with t as
(select date_1,
count_1,
row_number() over(order by date_1) 'rn'
from wh)
select a.date_1,
a.count_1,
case when b.rn is null or a.count_1>b.count_1 then '上升'
else '下降' end 'status_1'
from t a
left join t b on a.rn=b.rn+1
/*
date_1 count_1 status_1
---------- ----------- --------
2014-12-01 1 上升
2014-12-02 3 上升
2014-12-03 5 上升
2014-12-04 3 下降
2014-12-05 2 下降
2014-12-06 1 下降
2015-01-01 5 上升
2015-01-02 4 下降
2015-01-03 3 下降
(9 行受影响)
*/