this is a problem i need to work out within the next few hours, so
i hope SOMEONE has some advice for me!
i have a content_items table, a content_items_open_map table
(just like the site_wide_category_map table, only this maps
content_items to other tables, including categories), and a
category_hierarchy table. here's the sql i'm querying with:
select distinct on (content_item_id) ci.content_item_id,
ci.headline, ci.source_id, ci.summary_a, ci.summary_b,
ci.summary_c, ci.review_score,
case when sysdate() between push_date and pull_date then
push_weight + mapping_weight
else mapping_weight end as mapping_weight,
case when ch.child_category_id = 1 then 0 else 1 end as
distance
from content_item_open_map ciom, content_items ci,
category_hierarchy ch
where ciom.on_which_table = 'categories' and ciom.on_what_id
= ch.child_category_id and (ch.parent_category_id = 1 or
ch.child_category_id = 1)
and push_weight > 0 and ci.active_p = 't'
and ci.content_item_id = ciom.content_item_id
order by content_item_id, distance asc, mapping_weight desc,
release_date desc, content_item_id limit 10;
in other words, i'm fetching all the content items that are mapped
to a category OR its child categories, and trying to sort them by
their mapping_weight (push_weight is just an addition to
mapping weight) and distance (either 0 or 1) from the category
specified (in this case, category_id 1). the problem is, i don't
want duplicates, so i've selected with DISTINCT ON
content_item_id. i can't do a select like this, however, without
specifying content_item_id as the first argument to ORDER BY,
which completely ruins my sorting.
anyone have any ideas? thank you in advance for your help!