select y.nft_id,
       y.store,
       y.owner,
       y.meta_id,
       y.eth_price,
       y.token_price,
       y.metadata,
       y.metadata_uri,
       y.eth_for_sale,
       y.token_for_sale,
       x.dollar_price,
       y.inserted,
       z.cnt
from nft as y,
     (
         select coalesce(a1.store, b1.store)     as store,
                coalesce(a1.meta_id, b1.meta_id) as meta_id,
                coalesce(a1.nft_id, b1.nft_id)   as nft_id,
                a1.dollar_price
         from (
                  select coalesce(eth_nfts.store, token_nfts.store)               as store,
                         coalesce(eth_nfts.meta_id, token_nfts.meta_id)           as meta_id,
                         coalesce(eth_nfts.nft_id, token_nfts.nft_id)             as nft_id,
                         eth_nfts.min_eth_price,
                         token_nfts.min_token_price,
                         coalesce(eth_nfts.dollar_price, token_nfts.dollar_price) as dollar_price
                  from (select distinct on (n1.store, n1.meta_id) n1.store,
                                                                  n1.meta_id,
                                                                  n1.nft_id,
                                                                  min(n1.eth_price)                                            as min_eth_price,
                                                                  min(n1.eth_price) / (10 ^ 18) *
                                                                  (select val from lookup where key = 'ubiqUsdRatio')::decimal as dollar_price
                        from nft n1
                        where n1.hidden = false
                          and n1.owner <> '0x0000000000000000000000000000000000000000'
                          and n1.metadata is not null
                          and n1.eth_for_sale = true
                        group by n1.store, n1.meta_id, n1.eth_price, n1.nft_id
                       ) as eth_nfts
                           full join
                       (select distinct on (n1.store, n1.meta_id) n1.store,
                                                                  n1.meta_id,
                                                                  n1.nft_id,
                                                                  min(n1.token_price)                                            as min_token_price,
                                                                  min(n1.token_price) / (10 ^ 18) *
                                                                  (select val from lookup where key = 'ubiqUsdRatio')::decimal /
                                                                  (select val from lookup where key = 'ubiqGransRatio')::decimal as dollar_price
                        from nft n1
                        where n1.hidden = false
                          and n1.owner <> '0x0000000000000000000000000000000000000000'
                          and n1.metadata is not null
                          and n1.token_for_sale = true
                        group by n1.store, n1.meta_id, n1.token_price, n1.nft_id
                       ) as token_nfts
			on eth_nfts.store = token_nfts.store and eth_nfts.meta_id = token_nfts.meta_id
              ) as a1
                  full join
              (
                  select any_nfts.store, any_nfts.meta_id, min(any_nfts.nft_id) as nft_id
                  from nft as any_nfts
                           inner join (
                      select nosale_nfts.store,
                             nosale_nfts.meta_id,
                             array_agg(nosale_nfts.token_for_sale),
                             sum(case when nosale_nfts.token_for_sale = true then 1 else 0 end) as num_tokens_for_sale,
                             sum(case when nosale_nfts.eth_for_sale = true then 1 else 0 end)   as eth_tokens_for_sale
                      from nft as nosale_nfts
                      where nosale_nfts.metadata is not null
                      group by nosale_nfts.store, nosale_nfts.meta_id
                      having sum(case when nosale_nfts.token_for_sale = true then 1 else 0 end) = 0
                         and sum(case when nosale_nfts.eth_for_sale = true then 1 else 0 end) = 0
                      order by nosale_nfts.store, nosale_nfts.meta_id
                  ) as ag
                                      on ag.store = any_nfts.store and ag.meta_id = any_nfts.meta_id
                  where any_nfts.metadata is not null
                  group by any_nfts.store, any_nfts.meta_id
              ) as b1
              on a1.nft_id = b1.nft_id
     ) as x,
     (
         select i.meta_id, i.store, a.verified, count(*) as cnt
         from nft as i, store as s, account as a
         where i.metadata is not null
           and i.hidden = false
           and i.owner <> '0x0000000000000000000000000000000000000000'
           and s.address = i.store
           and a.id = s.creator
           OWNER_CLAUSE
         group by i.meta_id, i.store, a.verified
     ) as z
where x.nft_id = y.nft_id
  and x.store = z.store
  and x.meta_id = z.meta_id
