postgresql 7.2 SELECT cr.item_id as glossar_id , crr.title , crr.description , gl.source_category_id , gl.target_category_id , gl.owner_id, case when gl.target_category_id is null then 0 else 1 end as sort_key FROM gl_glossars gl, cr_items cr, cr_revisions crr WHERE cr.latest_revision = crr.revision_id AND crr.revision_id = gl.glossar_id AND gl.owner_id = :owner_id [template::list::orderby_clause -orderby -name gl_glossar] select count(ci.item_id) from cr_items ci where ci.parent_id = :glossar_id