SQL,根据时间(列)选择行,当另一列具有特定值时发生时间

时间:2016-06-22 19:24:38

标签: sql teradata

我得到的情况是带有温度传感器和发动机转速传感器的发动机

我可以在发动机过热时选择关闭时间

SELECT global_times AS times
FROM myTable AS A
WHERE ser_num LIKE 'XXXX' --user changes this
                     AND chan_nm IN ('temp_oil')
                     AND  global_times BETWEEN TIMESTAMP '2016-01-14 00:00:01'   AND TIMESTAMP '2016-01-14 00:59:59' --just working with a day at a time
                     AND EVNT_VAL>=90; 

因此,基本上,当chan_nm为“temp_oil”

时,event_value对应于温度

但是当chan_nm是“engine_speed”

时,它对应于rpms

此代码采用此表:

SER_NUM global_times    EVNT_VAL    CHAN_NM
'XXXX'  00:00:01.01     700 ENGSPD
'XXXX'  00:00:01.01     87  temp_oil
'XXXX'  00:00:02.01     701 ENGSPD
'XXXX'  00:00:02.01     88  temp_oil
'XXXX'  00:00:03.01     702 ENGSPD
'XXXX'  00:00:03.01     89  temp_oil
'XXXX'  00:00:04.01     703 ENGSPD
'XXXX'  00:00:04.01     90  temp_oil
'XXXX'  00:00:05.01     704 ENGSPD
'XXXX'  00:00:05.01     91  temp_oil
'XXXX'  00:00:06.01     704 ENGSPD
'XXXX'  00:00:06.01     92  temp_oil

并返回

global_times
00:00:04.01
00:00:05.01
00:00:06.01

我想做的是获取与该时间对应的所有列,特别是与引擎速度对应的行

我尝试过使用连接,但无法弄清楚如何右/左/联合表格

我也尝试过做

select * from my_table where global_time in (XXXX)

我通过复制/粘贴上一个代码来替换XXX

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要温度高的ENGSPDSELECT * FROM myTable AS A WHERE ser_num LIKE 'XXXX' --user changes this AND chan_nm IN ('temp_oil', `ENGSPD`) AND global_times BETWEEN TIMESTAMP '2016-01-14 00:00:01' AND TIMESTAMP '2016-01-14 00:59:59' --just working with a day at a time QUALIFY MAX(CASE WHEN chan_nm = 'temp_oil' THEN EVNT_VAL END) OVER (PARTITION BY global_times) >=90; 。您可以使用组计数在单个查询中执行此操作:

{{1}}