display data according to the row number in a table

sql postgresql

19 观看

1回复

39 作者的声誉

I have the following table and some animals have more than 10 rows of data. For a particular animal (animals_key) how can display a particular row. In our example lets say row 2.

Animal Breeding table

作者: TechPathi 的来源 发布者: 2017 年 9 月 15 日

回应 1


1

276593 作者的声誉

You can try using row_number() here with a partition on the animal and a descending ordering based on the pcnt column.

select
    t.soc_code,
    t.animals_key,
    t.breed,
    t.pcnt,
    t.create_method,
    t.create_date,
    t.create_user_id
from
(
    select t.*, row_number() over (partition by animals_key order by pcnt desc) rn
    from your_table t
    where animals_key = 211004650
) t
where t.rn = 2;
作者: Tim Biegeleisen 发布者: 2017 年 9 月 15 日
32x32