Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 重复值的密集秩_Sql_Psql_Window Functions_Gaps And Islands_Dense Rank - Fatal编程技术网

Sql 重复值的密集秩

Sql 重复值的密集秩,sql,psql,window-functions,gaps-and-islands,dense-rank,Sql,Psql,Window Functions,Gaps And Islands,Dense Rank,sql希望如何获得这种密集的秩?使用lag计算前一个值是什么,然后是一个累积和: id val dense_rank 1 11 => 1 2 11 1 3 22 2 4 33 3 5 33 3 6 11 4 他是一把小提琴 select t.*, count(*) filter (w

sql希望如何获得这种密集的秩?

使用lag计算前一个值是什么,然后是一个累积和:

id     val       dense_rank
1       11   =>     1
2       11          1
3       22          2
4       33          3
5       33          3
6       11          4
他是一把小提琴

select t.*,
       count(*) filter (where prev_val is null or prev_val <> val) over (order by id) as dense_rank
from (select t.*, lag(val) over (order by id) as prev_val
      from t
     ) t;