使用IN关键字db2

时间:2017-03-21 05:19:38

标签: db2 dashdb

所以这是我的问题:

select count(a.rs1) as partsProduced, a.DEVICEID ,a.DATA_MAINPROGRAMNO from (
    SELECT currow, rs1, rs2,DEVICEID,DATA_MAINPROGRAMNO
    FROM (
    SELECT current_row.row1  currow,
     current_row.DATA_RUNSTATUS rs1, 
     current_row.DEVICEID DEVICEID,
      current_row.DATA_MAINPROGRAMNO DATA_MAINPROGRAMNO,
     next_row.row1 nextrow,
     next_row.DATA_RUNSTATUS rs2
    FROM (SELECT rownumber() over(order by  DATA_TIMEINMILLISECS) as row1, m.* FROM "IOTP_SL2IID_GOVHISTORICALDB_2017-03-20"
    m where DEVICEID ='deviceName' ) AS current_row
    LEFT JOIN (SELECT rownumber() over(order by DATA_TIMEINMILLISECS) as row1, m.* FROM "IOTP_SL2IID_GOVHISTORICALDB_2017-03-20" m 
    where DEVICEID ='deviceName' ) AS next_row 
    ON (current_row.row1 = next_row.row1 - 1)
    where (TIMESTAMP_FORMAT(current_row.DATA_TIMESTAMP,'YYYY.MM.DD.HH24.MI.SS') 
    BETWEEN '2017-03-20 14:00:00' AND '2017-03-20 15:00:00')) AS a WHERE a.rs1 = 3 AND a.rs2 = 0) as a group by a.DEVICEID ,a.DATA_MAINPROGRAMNO;

我得到了正确的输出,其中生成的部件在给定的小时内是17,但每当我将DEVICEID= 'deviceName'替换为

DEVICEID IN ( select d.DEVICE_ID  from device d  left join line l  on ( l.id=d.LINE_ID ) where l.id=1  

因为我需要多个deviceNames并使用它启动查询,现在我为同一个设备获得了300个东西,我之前用单个设备运行,所以我使用IN关键字正确吗?或者有任何编辑要做吗?

1 个答案:

答案 0 :(得分:0)

的Rahul,

确保表设备中的DEVICE_ID值不为空。如果值不为null,则可以继续使用现有查询:

DEVICEID IN(从设备d中选择d.DEVICE_ID左边的连接线l(l.id = d.LINE_ID),其中l.id = 1

如果DEVICE_ID具有空值,则使用以下查询

DEVICEID IN(从设备d选择不同的nvl(d.DEVICE_ID,0)左连接线l on(l.id = d.LINE_ID)其中l.id = 1