我有一个具有以下结构的表
UpdateDate Rate
2015-08-26 00:00:00.000 310.000000
2016-06-02 00:00:00.000 310.000000
2017-02-01 00:00:00.000 310.000000
2017-09-15 00:00:00.000 320.000000
2018-01-31 00:00:00.000 310.000000
2018-02-16 00:00:00.000 310.000000
2018-02-23 00:00:00.000 310.000000
2018-03-09 00:00:00.000 310.000000
2018-04-15 00:00:00.000 320.000000
我正在通过FromDate作为报告标准。
我只想获取“ FromDate”条件后“比率”字段大于先前比率的那些行。
例如,如果我通过2018-04-01作为FromDate选择标准, 那么我将得到以下输出,因为2018-04-15的速率大于2018-03-09的速率
2018-04-15 00:00:00.000 320.000000
但是如果我将2018-05-01作为FromDate参数传递,那么我将不会得到任何输出
价格从2018-05-01直到今天都没有更新。
谢谢。
答案 0 :(得分:0)
这使用窗口功能:
declare @test table ([UpdateDate] datetime, [Rate] Decimal(9,6))
insert into @test values
('2015-08-26 00:00:00.000', 310.000000)
, ('2016-06-02 00:00:00.000', 310.000000)
, ('2017-02-01 00:00:00.000', 310.000000)
, ('2017-09-15 00:00:00.000', 320.000000)
, ('2018-01-31 00:00:00.000', 310.000000)
, ('2018-02-16 00:00:00.000', 310.000000)
, ('2018-02-23 00:00:00.000', 310.000000)
, ('2018-03-09 00:00:00.000', 310.000000)
, ('2018-04-15 00:00:00.000', 320.000000)
declare @date as date = '2018-04-01'
;with cte1 as (select [UpdateDate], [Rate], iif(lag(Rate) OVER (ORDER BY [UpdateDate]) < [Rate], 1, 0) as [Changed] from @test),
cte2 as (Select top 1 * from cte1 where [UpdateDate] > @date order by [UpdateDate])
select [UpdateDate], [Rate] from cte2 where [Changed] = 1
set @date = '2018-03-01'
;with cte1 as (select [UpdateDate], [Rate], iif(lag(Rate) OVER (ORDER BY [UpdateDate]) < [Rate], 1, 0) as [Changed] from @test),
cte2 as (Select top 1 * from cte1 where [UpdateDate] > @date order by [UpdateDate])
select [UpdateDate], [Rate] from cte2 where [Changed] = 1
答案 1 :(得分:0)
谢谢,已经解决了。
- /bin/bash -c "source ./.ci_config/industrial_ci/src/tests/gitlab_module.sh"
- pass_sshkey_docker