Sql 逻辑返回被删除的行,而不是保留的行。 ID Date Value 1 2015-01-01 1 2 2015-01-02 1 <redundant 3 2015-01-03 2 4
逻辑返回被删除的行,而不是保留的行。Sql 逻辑返回被删除的行,而不是保留的行。 ID Date Value 1 2015-01-01 1 2 2015-01-02 1 <redundant 3 2015-01-03 2 4 ,sql,sql-server,window-functions,Sql,Sql Server,Window Functions,逻辑返回被删除的行,而不是保留的行。 ID Date Value 1 2015-01-01 1 2 2015-01-02 1 <redundant 3 2015-01-03 2 4 2015-01-05 2 <redundant 5 2015-01-06 1 6 2015-01-08 1 <redundant 7 2015-01-09 1 <redunda
ID Date Value
1 2015-01-01 1
2 2015-01-02 1 <redundant
3 2015-01-03 2
4 2015-01-05 2 <redundant
5 2015-01-06 1
6 2015-01-08 1 <redundant
7 2015-01-09 1 <redundant
8 2015-01-10 2
9 2015-01-11 3
10 2015-01-12 3 <redundant
SELECT ID, Date, Value,
RANK() over(partition by Value order by Date ASC) Rank,
FROM DataLogging
ORDER BY Date ASC
ID Date Value Rank (Rank)
1 2015-01-01 1 1 (1)
2 2015-01-02 1 2 (2)
3 2015-01-03 2 1 (1)
4 2015-01-05 2 2 (2)
5 2015-01-06 1 3 (1)
6 2015-01-08 1 4 (2)
7 2015-01-09 1 5 (3)
8 2015-01-10 2 3 (1)
9 2015-01-11 3 1 (1)
10 2015-01-12 3 2 (2)
select *
from ( select ID, Date, Value, lag(Value, 1, 0) over (order by ID) as ValueLag
from table ) tt
where ValueLag is null or ValueLag <> Value
declare @tt table (id tinyint, val tinyint);
insert into @tt values
( 1, 1),
( 2, 1),
( 3, 2),
( 4, 2),
( 5, 1),
( 6, 1),
( 7, 1),
( 8, 2),
( 9, 3),
(10, 3);
select id, val, LAG(val) over (order by id) as lagVal
from @tt;
-- find the good
select id, val
from ( select id, val, LAG(val) over (order by id) as lagVal
from @tt
) tt
where lagVal is null or lagVal <> val
-- select the bad
select tt.id, tt.val
from @tt tt
left join ( select id, val
from ( select id, val, LAG(val) over (order by id) as lagVal
from @tt
) ttt
where ttt.lagVal is null or ttt.lagVal <> ttt.val
) tttt
on tttt.id = tt.id
where tttt.id is null
WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER(ORDER BY [Date]) RN1,
ROW_NUMBER() OVER(PARTITION BY Value ORDER BY [Date]) RN2
FROM dbo.YourTable
), CTE2 AS
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY Value, RN1 - RN2 ORDER BY [Date]) N
FROM CTE
)
SELECT *
FROM CTE2
ORDER BY ID;
╔════╦════════════╦═══════╦═════╦═════╦═══╗
║ ID ║ Date ║ Value ║ RN1 ║ RN2 ║ N ║
╠════╬════════════╬═══════╬═════╬═════╬═══╣
║ 1 ║ 2015-01-01 ║ 1 ║ 1 ║ 1 ║ 1 ║
║ 2 ║ 2015-01-02 ║ 1 ║ 2 ║ 2 ║ 2 ║
║ 3 ║ 2015-01-03 ║ 2 ║ 3 ║ 1 ║ 1 ║
║ 4 ║ 2015-01-05 ║ 2 ║ 4 ║ 2 ║ 2 ║
║ 5 ║ 2015-01-06 ║ 1 ║ 5 ║ 3 ║ 1 ║
║ 6 ║ 2015-01-08 ║ 1 ║ 6 ║ 4 ║ 2 ║
║ 7 ║ 2015-01-09 ║ 1 ║ 7 ║ 5 ║ 3 ║
║ 8 ║ 2015-01-10 ║ 2 ║ 8 ║ 3 ║ 1 ║
║ 9 ║ 2015-01-11 ║ 3 ║ 9 ║ 1 ║ 1 ║
║ 10 ║ 2015-01-12 ║ 3 ║ 10 ║ 2 ║ 2 ║
╚════╩════════════╩═══════╩═════╩═════╩═══╝
DELETE FROM CTE2
WHERE N > 1;
with todelete as (
select t.*, lag(value) over (order by date) as prev_value
from t
)
delete from todelete
where value = prev_value;
with todelete as (
select t.*, lag(value) over (order by date) as prev_value
from t
)
select *
from todelete
where value <> prev_value or prev_value is null;
;WITH Ordered AS
(
SELECT ROW_NUMBER() OVER (ORDER BY [Date]) AS [Row], *
FROM DataLogging
),
Final AS
(
SELECT
o1.*, NULLIF(o1.Value - ISNULL(o2.Value, o1.Value - 1), 0) [Change]
FROM
Ordered o1
LEFT JOIN Ordered o2 ON
o1.[Row] = o2.[Row] + 1
)
SELECT * FROM Final
DELETE FROM DataLogging where Change IS NULL