SELECT A, row_num,
CASE WHEN FIRST_VALUE(row_num) OVER(PARTITION BY A ORDER BY row_num) = row_num
THEN 1 ELSE 0 END AS first_row,
CASE WHEN FIRST_VALUE(row_num) OVER(PARTITION BY A ORDER BY row_num DESC)=row_num
THEN 1 ELSE 0 END AS last_row
FROM tab
ORDER BY a, row_num;
select
a,
row_number,
case ra = 1 then 1 else 0 end as first,
case rd = 1 then 1 else 0 end as last
from (
select
a,
row_number,
row_number() over(partition by a order by row_number) as ra,
row_number() over(partition by a order by row_number desc) as rd
from my_table
) x
order by a
3条答案
按热度按时间gg58donl1#
你可以用
FIRST_VALUE
:db<>小提琴演示
输出:
tyg4sfes2#
这将显示总计数:
您还可以执行以下操作:
按降序排序意味着1=您的最后一条记录
zlwx9yxi3#
我会做: