sql - How to select several columns in Postgres without group by all columns -
i'm using postgres 8.4.2 , i'm trying make query selects data wrong results when group these several columns select.
i tried not group of these columns, notice, should group selected columns.
my query looks that:
select c.id category_id, c.parent_id, c.title category_title, count(ofrs.id) offers_count categories c left join offers_to_categories otc on c.id = otc.category_id left join offers ofrs on otc.offer_id = ofrs.id ofrs.offer_type = 1 , ofrs.status = 1 group c.id, c.title, c.parent_id; i want select offers count category offer_type = 1.
how without group several columns group c.id?
i tried following window function result same - shows me more results should be.
select ofr.id , c.id category_id, c.parent_id, c.title category_title,ofr.website_id , count( ofr.id) on (partition (ofr.id) order ofr.id) offers ofr inner join offers_to_categories ofr_cat on (ofr_cat.offer_id = ofr.id) inner join categories c on (c.id = ofr_cat.category_id) (c.id = 3 or c.parent_id = 3) , ofr.website_id = 1 , ofr.status = 1
i found answer question , is:
select distinct on(ofr.id) ofr.id , c.id category_id, c.parent_id, c.title category_title,ofr.website_id , count( ofr.id) on (partition (select distinct on(ofr.id) ofr.id offers id group ofr.id) order ofr.id) offers ofr inner join offers_to_categories ofr_cat on (ofr_cat.offer_id = ofr.id) inner join categories c on (c.id = ofr_cat.category_id) (c.id = 3 or c.parent_id = 3) , ofr.website_id = 1 , ofr.status = 1
Comments
Post a Comment