简体   繁体   中英

finding if group of n consecutive numbers are repeating in SQL

I want to find out if a set of n consecutive numbers are repeating for a particular partition.

Lets say I have deviceIds with some recordid whose value are in consecutive fashion each record that device generates is incremented by 1.

So I want to know if resetting the recordid and regenerating records with same sequence again. Lets understand by following table.

Device ID        recordid             DateTime
--------------------------------------------------
07777778999       2               18-12-2016 17:15
07777778123       10              18-12-2016 18:10
07777778123       10              18-12-2016 18:20
07777778999       3               19-12-2016 19:30
07777778999       4               19-12-2016 12:15
07777778999       5               19-12-2016 13:15
07777778999       6               20-12-2016 11:15
07777778123       11               20-12-2016 9:15
07777778123       12               20-12-2016 17:15
07777778123       13               20-12-2016 17:25
07777778123       14               20-12-2016 17:35
07777778999       7                20-12-2016 17:45
07777778999       8               20-12-2016 17:55
07777778999       4               20-12-2016 18:50
07777778999       5               20-12-2016 18:55 
07777778999       6               20-12-2016 18:50
07777778999       7               20-12-2016 18:55
...

There are many devices so we can partition it by devices and then apply our query to find if for a device, the numbers are regenerated like for example 07777778999, we had record id's 2,3,4,5,6,7,8 and then the numbers are regenerated from 4,5,6,7, etc.

So i want to list down all the devices with such regeneration using SQL. So the expected output for the above would be device 07777778999

Try this.

This will return all the records, which has the regenerated values.

SELECT DeviceID,RecordId
FROM ResultSet
GROUP BY DeviceID,RecordId
HAVING COUNT(1)>1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM