SQL Ignoring duplicate values if ID difference larger than - sql

Lets say I have a simple table:
ID value
1 15
2 30
3 **10**
4 **10**
5 16
6 20
7 **15**
8 **15**
9 40
10 70
11 **50**
12 **50**
13 19
14 11
15 3
My select should ignore consecutive double values. I know how to do that - I am using lead function
But this eliminates all consecutive duplicates.
SELECT [DetectorParameterValue]
FROM (
SELECT lead(DetectorParameterValue,1) over (partition by runid order by runtime) AS prev_DetectorParameterValue
FROM table_Detector
WHERE RunID = #run_id
) AS [InnerDetector]
WHERE (prev_DetectorParameterValue is null or or prev_DetectorParameterValue <> DetectorParameterValue
But it should ignore them only if ID diff is more than 5.
So my select should be
ID Value
1 15
2 30
3 10
5 16
6 20
7 15
8 15
9 40
10 70
11 50
13 19
14 11
15 3
ID 4 and 12 should be ignored but ID 8 should not because ID 8 - ID 4 is not > 5.
Is there a way to do this?
Thanks in advance.

This is a gaps and islands problem in disguise. One approach uses the difference in row numbers method.
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY ID) rn1,
ROW_NUMBER() OVER (PARTITION BY value ORDER BY ID) rn2
FROM yourTable
),
cte2 AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY value, rn1 - rn2 ORDER BY ID) rn
FROM cte
)
SELECT ID, value
FROM cte2
WHERE rn = 1
ORDER BY ID;

Related

Find first N rows that have unique value with mod(id, N)

For example, N is 10 and a table looks like
id
1
2
3
4
5
6
7
10
11
12
13
108
109
111
112
113
Need to find first N rows that have unique value with mod(id, N).
Expected result is
mod10
1
2
3
4
5
6
7
10
108
109
I've tried something like
select *
from
(
select id, id % 10 as seq_id
from accounts order by id
) as s1
group by s1.seq_id limit 10;`
but not working.
You can use window function here -
SELECT id
FROM (SELECT id, ROW_NUMBER() OVER(PARTITION BY id % 10 ORDER BY id) RN
FROM table_name
) X
WHERE RN = 1
ORDER BY id
You can try the below one - using row_number()
DEMO
with cte as
(
select id, row_number() over(partition by seq_id order by id) as rn
from
(
select id, id % 10 as seq_id from tablename
)A
)
select id from cte where rn=1 order by id
OUTPUT:
id
1
2
3
4
5
6
7
10
108
109

What is the most efficient SQL query to find the max N values for every entities in a table

I wrote these 2 queries, the first one is keeping duplicates and the second one is dropping them
Does anyone know a more efficient way to achieve this?
Queries are for MSSQL, returning the top 3 values
1-
SELECT TMP.entity_id, TMP.value
FROM(
SELECT TAB.entity_id, LEAD(TAB.entity_id, 3, 0) OVER(ORDER BY TAB.entity_id, TAB.value) AS next_id, TAB.value
FROM mytable TAB
) TMP
WHERE TMP.entity_id <> TMP.next_id
2-
SELECT TMP.entity_id, TMP.value
FROM(
SELECT TMX.entity_id, LEAD(TMX.entity_id, 3, 0) OVER(ORDER BY TMX.entity_id, TMX.value) AS next_id, TMX.value
FROM(
SELECT TAB.entity_id, LEAD(TAB.entity_id, 1, 0) OVER(ORDER BY TAB.entity_id, TAB.value) AS next_id, TAB.value, LEAD(TAB.value, 1, 0) OVER(ORDER BY TAB.entity_id, TAB.value) AS next_value
FROM mytable TAB
) TMX
WHERE TMP.entity_id <> TMP.next_id OR TMX.value <> TMX.next_value
) TMP
WHERE TMP.entity_id <> TMP.next_id
Example:
Table:
entity_id value
--------- -----
1 9
1 11
1 12
1 3
2 25
2 25
2 5
2 37
3 24
3 9
3 2
3 15
Result Query 1 (25 appears twice for entity_id 2):
entity_id value
--------- -----
1 9
1 11
1 12
2 25
2 25
2 37
3 9
3 15
3 24
Result Query 2 (25 appears only once for entity_id 2):
entity_id value
--------- -----
1 9
1 11
1 12
2 5
2 25
2 37
3 9
3 15
3 24
You can use the ROW_NUMBER which will allow duplicates as follows:
select entity_id, value from
(select t.*, row_number() over (partition by entity_id order by value desc) as rn
from your_Table) where rn <= 3
You can use the rank to remove the duplicate as follows:
select distinct entity_id, value from
(select t.*, rank() over (partition by entity_id order by value desc) as rn
from your_Table) where rn <= 3

Escaping x number of rows in Select query

I want rows from the entire table but I only want to select rows 1,5,10,15,20,25
Table
ID Col1 Col2……….
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
I want to select in my result set
Id Col1 Col2……….
1
5
10
15
20
Any suggestions would be appreciated.
Thank you
You can use:
where id = 1 or
id % 5 = 0
The % is the modulo operator. Some databases use a mod() function instead.
If id has gaps or other issues, you may want to do this with row_number():
select t.*
from (select t.*, row_number() over (order by id) as seqnum
from t
) t
where seqnum = 1 or seqnm % 5 = 0;

Remove minimum rank rows in SQL Server

I have a table like below.
Customer Order Rank
1 12 3
1 14 7
2 15 6
2 16 4
2 17 2
2 21 1
3 24 5
3 25 6
3 27 7
Now, I want to select all rows except for rows with minimum ranks for each customer. It should look like below.
Customer Order Rank
1 14 7
2 15 6
2 16 4
2 17 2
3 25 6
3 27 7
You can use a CTE + ROW_NUMBER:
WITH CTE AS
(
SELECT Customer, [Order], Rank,
RN = ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Rank)
FROM dbo.Customers
)
SELECT Customer, [Order], Rank
FROM CTE
WHERE RN > 1
ORDER BY Customer, Rank DESC
Demo: http://sqlfiddle.com/#!6/444be/3/0
WITH CTE AS (
SELECT Customer,Order,Rank,
ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Rank ) as rn FROM t
)
SELECT Customer,Order,Rank FROM CTE
WHERE rn >1

Counting number of sorted subgroups inpostgresql

I would like to count the number of sorted subgroups in the table below :
id nmb
1 11
2 12
3 13
4 22
5 23
6 31
7 32
8 33
9 11
10 12
11 13
12 12
13 13
14 21
15 22
16 11
17 12
18 13
19 14
And want to obtain something like this in postgresql 8.4 :
id nmb local
1 11 1
2 12 1
3 13 1
4 22 1
5 23 1
6 31 1
7 32 1
8 33 1
9 11 2
10 12 2
11 13 2
12 12 3
13 13 3
14 21 3
15 22 3
16 11 4
17 12 4
18 13 4
19 14 4
EDIT: the last few numbers on the 'local' column were wrong. Corrected!
Thank you.
I though i finally understood what you want, consecutively growing values:
select id, nmb,
sum(flag)
over (order by id
rows unbounded preceding) as local
from
(
select
id, nmb,
case
when lag(nmb)
over (order by id) < nmb
then 0
else 1
end as flag
from t
) as dt
order by id
But group 4 doesn't fit
Edit: Now they fit :-)
It appears that you are trying to enumerate the groups, where a group starts with a lower nmb value than the previous row ("previous" defined by the id order).
The idea is to identify the start of a group by using lag(). Then take the cumulative sum to get the group identifier that you want:
select id, nmb, sum(StartFlag) over (order by id) as local
from (select id, nmb,
lag(nmb) over (order by id) as lastnmb,
(case when lag(nmb) over (order by id) < nmb then 0
else 1
end) as StartFlag
from t
) t
order by id;
SQL Fiddle
select
id, nmb,
row_number() over(partition by nmb order by id)
from t
order by id