ПФ
Size: a a a
ПФ
ПФ
GT
NB
(1, "a"),
(2, "b"),
(3, "a")
,
(4, "a"),
t
as (
Select 1
as a,
'a' as b
UNION ALL2
as a,
'b'as b
UNION ALL3
as a,
'a' as b
UNION ALL4
as a,
'a' as b
union all5
as a,
'a' as b
)
select t1.a, t1.b,
count(t2.a)
from t t1
leftt t2
on t1.b = t2.b
and t1.a > t2.a
and t1.a - 2 <= t2.a
group by t1.a, t1.b
А
t
as (
Select 1
as a,
'a' as b
UNION ALL2
as a,
'b'as b
UNION ALL3
as a,
'a' as b
UNION ALL4
as a,
'a' as b
union all5
as a,
'a' as b
)
select t1.a, t1.b,
count(t2.a)
from t t1
leftt t2
on t1.b = t2.b
and t1.a > t2.a
and t1.a - 2 <= t2.a
group by t1.a, t1.b
ПФ
ПФ
val win = Window.orderBy($"_1").rowsBetween(-2, -1)
df.withColumn("tst", collect_list($"_2").over(win)).withColumn("tst2", size(expr("filter(tst, x -> x == _2)")))
ПФ
А
ПФ
ПФ
ПФ
M
ПФ
M
А
ПФ
ПФ
SELECT *,
COUNT(*) OVER(PARTITION BY _2 ORDER BY _1
RANGE BETWEEN 2 PRECEDING AND CURRENT ROW
) -1 AS count
FROM table
ORDER BY ID
ПФ
ПФ