sql - Select a group of records based on one record in the group -
i seem stuck on problem. using crystal reports 2008 pull list of records ms-sql database. there 2 tables involved (they contain order data subscribers). first table ordermst , second orderdtl. joined 2 fields, account , subnumber. each account has many subnumbers , each subnumber has many invoicenumbers. each invoice row has column tells me wether or not it's recent invoice. need @ record determine whether customer active, cancelled, or expired. then, depending on status need select invoices subnumber. i'm stuck trying figure out way that.
here's example:
ordermst:
account subnumber pub 72781651 0025 navl 72781651 0012 ryir 72781651 0001 rhcs 80156287 0015 vgfa 80156287 0012 navl
orderdtl:
account subnumber invoicenumber pubstatus renewalthere 72781651 0025 15894578 0 72781651 0025 15754897 r 1 72781651 0025 15753412 r 1 72781651 0012 15753357 c 0 72781651 0012 15749875 r 1 72781651 0001 15465874 x 0 72781651 0001 15425789 r 1 80156287 0015 15656738 0 80156287 0012 15387956 c 0 80156287 0012 15324568 r 1
so, if looking count of invoices active subscriptions, select {orderdtl.renewalthere} = 0
, , report results show account 72781651 subnumber 0025 has 3 invoices , account 80156287 subnumber 0015 has 1 invoice. i'm stuck. need use 1 invoice level record tell me subscription want, , grab invoice level records invoice. ideas?
what want semi-join:
select account, subnumber, count(*) orderdtl exists ( select * orderdtl a.account = orderdtl.account , a.subnumber = orderdtl.subnumber , a.pubstatus = 'a' , a.renewalthere = 0 ) group account, subnumber
hth
Comments
Post a Comment