duplicate results on join sql -
i have 2 sql scripts. need combine data both of them. i've set them 2 seperate volatile tables join them finished report. script below example of i'm trying extract data. true script complex, , has many sub queries meriting need use volitile table instead of regular join. join on "customer_acct".the purpose of script produce record of orders, , have column on whether or not refund has been applied said order, tied "customer_acct".
the result gives me
cust_acct product_ordered refund_amt 1111111 item#5 10.00 1111111 item#5 20.00 1111111 item#5 30.00 2222222 item#5 10.00 2222222 item#5 20.00 2222222 item#5 30.00 3333333 item#5 10.00 3333333 item#5 20.00 3333333 item#5 30.00
my script summarized below.
create volatile table orders, no log as( select p.customer_acct p.product_ordered etc. server p )with data primary index (p.customer_acct) on commit preserve rows; create volatile table refund, no log as( select c.customer_acct c.refund_amt c.date etc. server c )with data primary index (c.customer_acct) on commit preserve rows; select distinct a.cust_acct ,a.product_ordered ,b.refund_amt orders left join refund b on b.cust_accta = a.customer_acct ; drop table orders; drop table refund;
basically, script returning result every line in 'orders' line, , duplicating every possible value in 'refund' table, despite not having real matches.
any idea? apologize front, sql relatively new me , i'm trying expand knowledge.
it not join have problem with. need aggregate data refund table account number single row account number.
select distinct a.cust_acct ,a.product_ordered ,case when count(b.cust_accta)=0 'no refund' else 'refund' end refund orders left join refund b on b.cust_accta = a.customer_acct group a.customer_acct
or can use sum() refund amount:
select distinct a.cust_acct ,a.product_ordered ,sum(b.refund_amt) refund_amount orders left join refund b on b.cust_accta = a.customer_acct group a.customer_acct
Comments
Post a Comment