[英]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.我想找出一组 n 个连续数字是否对特定分区重复。
Lets say I have deviceIds
with some recordid
whose value are in consecutive fashion each record that device generates is incremented by 1.假设我有
deviceIds
带有一些recordid
其值是连续的,设备生成的每条记录都增加 1。
So I want to know if resetting the recordid
and regenerating records with same sequence again.所以我想知道是否再次重置
recordid
并重新生成相同序列的记录。 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.有很多设备,所以我们可以按设备对其进行分区,然后应用我们的查询来查找是否为设备重新生成数字,例如 07777778999,我们有记录 ID 的 2、3、4、5、6、7、8 和然后数字从 4、5、6、7 等中重新生成。
So i want to list down all the devices with such regeneration using SQL.所以我想使用 SQL 列出所有具有这种再生的设备。 So the expected output for the above would be device 07777778999
所以上面的预期输出将是设备 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.