SQL比较值并替换为数据库值

时间:2018-01-17 06:28:39

标签: sql-server

我有以下数据库查询

USE Runtime

declare @day date = getdate()
declare @today datetime = convert(datetime,concat(convert(varchar(max),@day),' 07:00'))
declare @yest datetime = dateadd(DD,-1,@today)

select datetime,tagname,value 
from AnalogHistory
where tagname like 'MPS_VSD%_SPEED.PVAI' and (tagname ='MPS_FI794071.PVAI' or tagname = 'MPS_FI794051.PVAI')
and datetime between @yest and @today
and wwResolution = '300000'

返回以下数据

2018-01-16 07:00:00.0000000 MPS_VSD1_SPEED.PVAI 61.5437278747559
2018-01-16 07:00:00.0000000 MPS_VSD1_TRQ_FROM_SPEED.PVAI    750.451904296875
2018-01-16 07:00:00.0000000 MPS_VSD2_SPEED.PVAI 63.0761566162109
2018-01-16 07:00:00.0000000 MPS_VSD2_TRQ_FROM_SPEED.PVAI    742.239990234375
2018-01-16 07:00:00.0000000 MPS_FI794071.PVAI   0.0443801879882813
2018-01-16 07:00:00.0000000 MPS_FI794051.PVAI   0.0189132690429688
2018-01-16 07:05:00.0000000 MPS_VSD1_SPEED.PVAI 61.6443176269531
2018-01-16 07:05:00.0000000 MPS_VSD1_TRQ_FROM_SPEED.PVAI    749.930480957031
2018-01-16 07:05:00.0000000 MPS_VSD2_SPEED.PVAI 62.8407287597656
2018-01-16 07:05:00.0000000 MPS_VSD2_TRQ_FROM_SPEED.PVAI    742.960021972656
2018-01-16 07:05:00.0000000 MPS_FI794071.PVAI   0.0407485961914063
2018-01-16 07:05:00.0000000 MPS_FI794051.PVAI   0.0189132690429688
2018-01-16 07:10:00.0000000 MPS_VSD1_SPEED.PVAI 61.4452743530273
2018-01-16 07:10:00.0000000 MPS_VSD1_TRQ_FROM_SPEED.PVAI    749.669799804688
2018-01-16 07:10:00.0000000 MPS_VSD2_SPEED.PVAI 62.7358551025391
2018-01-16 07:10:00.0000000 MPS_VSD2_TRQ_FROM_SPEED.PVAI    742.239990234375
2018-01-16 07:10:00.0000000 MPS_FI794071.PVAI   0.0443801879882813
2018-01-16 07:10:00.0000000 MPS_FI794051.PVAI   0.0189132690429688
2018-01-16 07:15:00.0000000 MPS_VSD1_SPEED.PVAI 61.5351638793945
2018-01-16 07:15:00.0000000 MPS_VSD1_TRQ_FROM_SPEED.PVAI    750.451904296875

我想要的是,同一日期,如果MPS_FI794071> 10,使用MPS_VSD1_SPEED.PVAI,否则,将值设置为0.(基本上比较同一列中的两个值)

最好的方法是什么?

我在思考内循环,但必须有一种更简单的方法来使用案例。

谢谢

1 个答案:

答案 0 :(得分:0)

解决方案已解决。

由于值在同一列中,因此我将数据转移到单独的列中。

然后用例创建一个由另一列或固定值

设置的新列
USE Runtime

declare @day date = getdate()
declare @today datetime = convert(datetime,concat(convert(varchar(max),@day),' 07:00'))
declare @yest datetime = dateadd(DD,-1,@today)

select *,
case when [MPS_FI794051.PVAI] < 5 then [MPS_VSD1_SPEED.PVAI] else 0 end as VSD133,
case when [MPS_FI794071.PVAI] < 5 then [MPS_VSD2_SPEED.PVAI] else 0 end as VSD232
from
(
select datetime,tagname,value 
from AnalogHistory
where tagname like 'MPS_VSD%_SPEED.PVAI' and (tagname ='MPS_FI794071.PVAI' or tagname = 'MPS_FI794051.PVAI')
and datetime between @yest and @today
and wwResolution = '300000'
) as le
PIVOT
(
sum(value)
for tagname in ([MPS_VSD1_SPEED.PVAI],[MPS_VSD2_SPEED.PVAI],[MPS_FI794071.PVAI],[MPS_FI794051.PVAI])
) as pvt




2018-01-16 07:00:00.0000000 61.5437278747559    63.0761566162109    0.0443801879882813  0.0189132690429688  61.5437278747559    63.0761566162109
2018-01-16 07:05:00.0000000 61.6443176269531    62.8407287597656    0.0407485961914063  0.0189132690429688  61.6443176269531    62.8407287597656
2018-01-16 07:10:00.0000000 61.4452743530273    62.7358551025391    0.0443801879882813  0.0189132690429688  61.4452743530273    62.7358551025391
2018-01-16 07:15:00.0000000 61.5351638793945    62.7722396850586    0.0443801879882813  0.0189132690429688  61.5351638793945    62.7722396850586
2018-01-16 07:20:00.0000000 61.4859390258789    62.7936401367188    0.0443801879882813  0.0189132690429688  61.4859390258789    62.7936401367188
2018-01-16 07:25:00.0000000 61.4003295898438    63.024787902832 0.0443801879882813  0.0189132690429688  61.4003295898438    63.024787902832