Query to get latest addresses holding NFT of a given policy id

Wanted to ask if anyone has a query to get all the latest addresses holding NFTs of a given policy id.
I would like write a query directly using postgress not using graphql.

Regards,
Adam.

You would need to walk the txout path for any given NFT until you come to an utxo

Do you have an example query how to do it for a single one?

Regards,
Adam

Here is an example PostgreSQL query on cardano-db-sync:

select
    encode(policy, 'hex') as policy
  , convert_from(name, 'UTF8') as name
  , sum(quantity) as quantity
  , address
  from utxo_view
  inner join ma_tx_out
    on utxo_view.id = ma_tx_out.tx_out_id
  where encode(policy, 'hex') = '2aa9c1557fcf8e7caa049fa0911a8724a1cdaf8037fe0b431c6ac664'
  group by policy, name, address
order by quantity desc
limit 3;                                                    
                          policy                          |   name    |  quantity   |                                                 address                                                 
----------------------------------------------------------+-----------+-------------+---------------------------------------------------------------------------------------------------------
 2aa9c1557fcf8e7caa049fa0911a8724a1cdaf8037fe0b431c6ac664 | PIGYToken | 30000000000 | addr1qx2778sj320g56cnxtwq4t0d5qdcg6medfa8ec7xkgkl2qpcr6jg8lc7uzl5g2dww8lwn25yxu5klfldj6jhwl4q38sqk3pwp3
 2aa9c1557fcf8e7caa049fa0911a8724a1cdaf8037fe0b431c6ac664 | PIGYToken | 21847998900 | addr1q87gzd5247yh865ql898pzgeyha626e07m3pkg5r560fkevcmpgc80zylxur0y6m7ukw3tg5vjsvyndx6f7pwxkx520qejxg0x
 2aa9c1557fcf8e7caa049fa0911a8724a1cdaf8037fe0b431c6ac664 | PIGYToken | 20000000000 | addr1v9e06msz5r2kn2ae2c5rzjdx02na5tcdar3nxuylags709c5u7vvf
2 Likes

Thank you very much for the information.

Regards,
Adam