22,209
社区成员
发帖
与我相关
我的任务
分享
;with t as (
select 1 as id, null as pid, value = 1.1
union all
select 2 as id, 1 as pid, value = 1.3
union all
select 2 as id, 1 as pid, value = 1.0
union all
select 2 as id, 1 as pid, value = 2.1
union all
select 2 as id, 1 as pid, value = 2.9
union all
select 2 as id, 1 as pid, value = 0.9
union all
select 7 as id, 2 as pid, value = 2.1
union all
select 7 as id, 2 as pid, value = 2.9
union all
select 7 as id, 2 as pid, value = 0.9
union all
select 3 as id, null as pid, value = 2.1
union all
select 5 as id, 3 as pid, value = 3.3
union all
select 5 as id, 3 as pid, value = 1.0
union all
select 5 as id, 3 as pid, value = 9.1
union all
select 5 as id, 3 as pid, value = 2.9
union all
select 5 as id, 3 as pid, value = 0.9
)
,vs as (
select * from t where pid is null
union all
select a.* from t a inner join vs b on a.pid = b.id where b.value < a.value and b.pid is null
)select * from vs
--RESULT
id pid value
--1 NULL 1.1
--3 NULL 2.1
--5 3 3.3
--2 1 1.3
select * from t where pid is null
union all
select a.* from t a inner join vs b on a.pid = b.id where b.value < a.value and b.pid is null
也就是递归遍历会好慢,能否找一条符合要求的就返回,而不是所有数据查出来再筛选其中的一条数据
;with t as (
select 1 as id, null as pid, value = 1.1
union all
select 2 as id, 1 as pid, value = 1.3
union all
select 2 as id, 1 as pid, value = 1.0
union all
select 2 as id, 1 as pid, value = 2.1
union all
select 2 as id, 1 as pid, value = 2.9
union all
select 2 as id, 1 as pid, value = 0.9
union all
select 7 as id, 2 as pid, value = 2.1
union all
select 7 as id, 2 as pid, value = 2.9
union all
select 7 as id, 2 as pid, value = 0.9
union all
select 3 as id, null as pid, value = 2.1
union all
select 5 as id, 3 as pid, value = 3.3
union all
select 5 as id, 3 as pid, value = 1.0
union all
select 5 as id, 3 as pid, value = 9.1
union all
select 5 as id, 3 as pid, value = 2.9
union all
select 5 as id, 3 as pid, value = 0.9
)
,vs as (
select * from t where pid is null
union all
select a.* from t a inner join vs b on a.pid = b.id where b.value < a.value and b.pid is null
)
SELECT * FROM (
SELECT *,ROW_NUMBER()OVER(PARTITION BY pid ORDER BY value) rn FROM vs
)t1 WHERE pid IS NULL OR rn=1
;with t as (
select 1 as id, null as pid, value = 1.1
union all
select 2 as id, 1 as pid, value = 1.3
union all
select 2 as id, 1 as pid, value = 1.0
union all
select 2 as id, 1 as pid, value = 2.1
union all
select 2 as id, 1 as pid, value = 2.9
union all
select 2 as id, 1 as pid, value = 0.9
union all
select 7 as id, 2 as pid, value = 2.1
union all
select 7 as id, 2 as pid, value = 2.9
union all
select 7 as id, 2 as pid, value = 0.9
union all
select 3 as id, null as pid, value = 2.1
union all
select 5 as id, 3 as pid, value = 3.3
union all
select 5 as id, 3 as pid, value = 1.0
union all
select 5 as id, 3 as pid, value = 9.1
union all
select 5 as id, 3 as pid, value = 2.9
union all
select 5 as id, 3 as pid, value = 0.9
),t1 AS (
SELECT *,ROW_NUMBER()OVER(PARTITION BY pid ORDER BY t.value DESC) rn FROM t
)
,vs as (
select * from t1 where pid is null
union all
select a.* from t1 a inner join vs b on a.pid = b.id where b.value < a.value and b.pid is NULL
)select * from vs WHERE vs.rn=1 OR pid IS NULL