Ada banyak cara untuk melakukannya, tetapi jika Anda ingin bermain dengan fungsi jendela, Anda bisa RANK()
atau DENSE_RANK()
bobot dengan id dan hilangkan peringkat pertama dalam kueri luar:
WITH cte AS (
SELECT id, ('{user,'||index-1||'}')::text[] as json_path, (value->'user_weight')::text::numeric AS weight
FROM user_table, jsonb_array_elements(json_field->'user')
WITH ordinality arr(value, index)
WHERE arr.value->>'user_name' IN ('Devang', 'Meet')
ORDER BY id, value->'user_weight' DESC
)
SELECT * FROM (
SELECT cte.*,
RANK() OVER (PARTITION BY id ORDER BY id,weight DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS r
FROM cte) j
WHERE r > 1;
Demo:db<>fiddle