sql server中的不同二进制数

时间:2017-06-05 06:39:21

标签: sql sql-server

我每隔10分钟就有一次点火状态10 如何得到明确的价值,就像我不会重复1和0号一样 如果它是1的日期时间,那么下一个0将显示,然后下一个将显示


    ignition status   date time
    1                 2017-06-04  00:00:13.000
    0                 2017-06-04  02:36:29.000
    1                 2017-06-04  07:43:52.000
    0                 2017-06-04  23:56:18.000
    
query 
select top 100 ignitionstatus,datetime from rawdata
actual output 
ignitionstatus  stringdatetime
1              2017-05-26 17:17:50.000
1              2017-05-26 17:18:10.000
0              2017-05-26 17:18:30.000
1              2017-05-26 17:18:50.000
0              2017-05-26 17:19:30.000
1              2017-05-26 17:19:10.000
1              2017-05-26 17:20:10.000
we required
1              2017-05-26 17:17:50.000
0              2017-05-26 17:18:30.000
1              2017-05-26 17:18:50.000
1              2017-05-26 17:19:10.000

1 个答案:

答案 0 :(得分:1)

这样的东西? (从注释中提供的查询中获取表名和列名:select ignitionstatus,stringdatetime from processedrawdata)这使用CTE和ROW_NUMBER向行添加连续的整数序列:

WITH numbered_ignition_states AS 
(
    SELECT   row_num = ROW_NUMBER() OVER ( ORDER BY stringdatetime ),
             ignitionstatus,
             stringdatetime
    FROM     processedrawdata 
    ORDER BY stringdatetime
)
SELECT i1.ignitionstatus, i1.stringdatetime
FROM numbered_ignition_states i1, numbered_ignition_states i2
WHERE i1.row_num = 1 OR i1.row_num = i2.row_num + 1 AND i1.ignitionstatus != i2.ignitionstatus;

原始建议,在提供更多信息之前,假设该表被称为ignition_states并且它具有连续的整数主键列id

SELECT i1.ignition_status, i1.date_time
FROM ignition_states i1, ignition_states i2
WHERE i1.id = 1 OR i1.id = i2.id + 1 AND i1.ignition_status != i2.ignition_status;