简体   繁体   中英

Getting other columns from distinct postgresql

select 
product_level_2_name, 
ir.item_role, ii.item_importance, 
sl2.store_level_2_name
from dimensions_new.product_level_2
left join (select distinct item_importance from dimensions_new.product_attributes) ii on 1=1
left join (select distinct store_level_2_name from dimensions_new.store_level_2 where store_level_2_name::integer > 0) sl2 on 1=1
order by product_level_2_name, item_role, item_importance desc, sl2.store_level_2_name

Given so gives me about 2k entries applying distinct on those fields, which is correct. My difficulity here is, now that I have these distinct list, how can I extract additional column called item_role , this is NOT what I want left join (select distinct item_role from dimensions_new.product_attributes) ir on 1=1

rather from the already distincted result, I want to extract item_role. If I add that, I will end up having 10k or so results, which is not what I want. I want to have 2k result, from the distinct list, I want to exact other fields. How can I rewrite this query, I am assuming I would need subqueries for this?

Similarly, I also want to be able to from given left join (select distinct store_level_2_name from dimensions_new.store_level_2 where store_level_2_name::integer > 0) sl2 on 1=1

In here I only have store_level_2_name, I want to be able to get store_level_2_id from dimensions_new.store_level_2

select 
product_level_2_name, 
ir.item_role, ii.item_importance, 
sl2.store_level_2_name
from dimensions_new.product_level_2
left join (select distinct item_importance from dimensions_new.product_attributes) ii on 1=1
left join (select distinct store_level_2_name from dimensions_new.store_level_2 where store_level_2_name::integer > 0) sl2 on 1=1
order by product_level_2_name, item_role, item_importance desc, sl2.store_level_2_name

Given so gives me about 2k entries applying distinct on those fields, which is correct. My difficulity here is, now that I have these distinct list, how can I extract additional column called item_role , this is NOT what I want left join (select distinct item_role from dimensions_new.product_attributes) ir on 1=1

rather from the already distincted result, I want to extract item_role. If I add that, I will end up having 10k or so results, which is not what I want. I want to have 2k result, from the distinct list, I want to exact other fields. How can I rewrite this query, I am assuming I would need subqueries for this?

Similarly, I also want to be able to from given left join (select distinct store_level_2_name from dimensions_new.store_level_2 where store_level_2_name::integer > 0) sl2 on 1=1

In here I only have store_level_2_name, I want to be able to get store_level_2_id from dimensions_new.store_level_2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM