I have a table with measurements. Measurement is done every minute. I need to select only rows having the same sample_value more than once consecutively for the same device_id.
Here are initial data:
sample_date sample_time device_id sample_value
20180701 1010 111 11
20180701 1011 111 12
20180701 1012 111 13
20180701 1013 222 11
20180701 1014 222 11
20180701 1015 222 12
20180701 1016 111 12
20180701 1017 111 11
20180701 1018 222 13
20180701 1019 222 12
20180701 1020 222 13
20180701 1021 222 12
20180701 1022 222 12
20180701 1023 111 12
20180701 1024 111 13
20180701 1025 111 13
20180701 1026 111 12
20180701 1027 111 13
20180701 1028 222 14
20180701 1029 222 13
20180701 1030 222 14
20180701 1031 222 14
20180701 1032 222 14
20180701 1033 222 14
20180701 1034 222 14
20180701 1035 222 14
20180701 1036 111 13
20180701 1037 111 13
20180701 1038 111 14
20180701 1039 111 13
This is result I'm looking for:
sample_date sample_time device_id sample_value
20180701 1013 222 11
20180701 1014 222 11
20180701 1021 222 12
20180701 1022 222 12
20180701 1024 111 13
20180701 1025 111 13
20180701 1030 222 14
20180701 1031 222 14
20180701 1032 222 14
20180701 1033 222 14
20180701 1034 222 14
20180701 1035 222 14
20180701 1036 111 13
20180701 1037 111 13
Here are test data:
IF OBJECT_ID('samples', 'U') IS NOT NULL
DROP TABLE samples;
create table samples (
sample_date int,
sample_time int,
device_id int,
sample_value int
)
insert samples
values
(20180701, 1010, 111, 11)
,(20180701, 1011, 111, 12)
,(20180701, 1012, 111, 13)
,(20180701, 1013, 222, 11)
,(20180701, 1014, 222, 11)
,(20180701, 1015, 222, 12)
,(20180701, 1016, 111, 12)
,(20180701, 1017, 111, 11)
,(20180701, 1018, 222, 13)
,(20180701, 1019, 222, 12)
,(20180701, 1020, 222, 13)
,(20180701, 1021, 222, 12)
,(20180701, 1022, 222, 12)
,(20180701, 1023, 111, 12)
,(20180701, 1024, 111, 13)
,(20180701, 1025, 111, 13)
,(20180701, 1026, 111, 12)
,(20180701, 1027, 111, 13)
,(20180701, 1028, 222, 14)
,(20180701, 1029, 222, 13)
,(20180701, 1030, 222, 14)
,(20180701, 1031, 222, 14)
,(20180701, 1032, 222, 14)
,(20180701, 1033, 222, 14)
,(20180701, 1034, 222, 14)
,(20180701, 1035, 222, 14)
,(20180701, 1036, 111, 13)
,(20180701, 1037, 111, 13)
,(20180701, 1038, 111, 14)
,(20180701, 1039, 111, 13)
select * from samples
Here is SQL I'm trying to use, but I don't know how to set correct partitioning.
select *
from (select sample_date,
sample_time,
device_id,
sample_value,
row_number() over (partition by sample_date,
device_id,
sample_value
order by sample_date,
sample_time,
device_id) as occurrence
from samples) t
where occurrence > 1
Similar topics:
Select statement to find duplicates on certain fields
How to find consecutive rows based on the value of a column?