Not sure if I fully understand your problem. Hopefully the following helps anyway:
As you use "group by" you will get one observation for each distinct combination of orig.unit_pk,cb_cntr_service_category.service_cat_desc,orig.so_pk.
The value for pk will be the minimum value per distinct combination of these 3 columns.
As you select in your where close orig.unit_pk = 54 and cb_cntr_service_category.service_cat_desc = 'Video' only rows with these two values are selected and joined.
(actually: I'm not sure whether the joins happen first and then the where clause subsets the result or the other way round. You might want to try and have the selections already applied during the joins in the "on.." part to improve performance).
The "order by" is not necessary, "group by" sorts already.
select
orig.unit_pk,
cb_cntr_service_category.service_cat_desc,
orig.so_pk,
min(sub_prod_pckg_pk) as pk
from
cb_subs_prod_pckg as orig
left outer join cb_cntr_service_type on
orig.service_type_code = cb_cntr_service_type.service_type_code
left outer join cb_cntr_service_category on
cb_cntr_service_category.service_cat_code = cb_cntr_service_type.service_cat_code
where
orig.so_pk is not null and
orig.unit_pk = 54 and
cb_cntr_service_category.service_cat_desc = 'Video'
group by
orig.unit_pk,
cb_cntr_service_category.service_cat_desc,
orig.so_pk
"....find the first occurance of an so_pk ..."
As you use "group by" you will only get one row per combination. As the only other variable is a minimum it doesn't matter whether this is "the first one" or not (the "first one" wouldn't be possible).
HTH
Patrick
... View more