Help with some SQL

Hi folks! 

I'm hoping one of your brains can help me. I'm trying to come up with a code that will help us with out audit that will return all amounts paid on specific GL's by batch. Very beginner here, but this is what I've come up with: 

select distinct O.customer_no, C.fname, C.lname, O.order_no, O.batch_no, O.created_by, O.create_dt, O.tot_ticket_paid_amt,
O.tot_fee_paid_amt, O.transaction_no, P.post_no, P.gl_act_no, G.gl_description
from T_ORDER as O
join T_BATCH as B on O.batch_no = B.batch_no
right join T_GL_POSTING_HISTORY as P on B.post_no = P.post_no
join T_CUSTOMER as C on O.customer_no = C.customer_no
join T_GL_ACCOUNT as G on P.gl_act_no =G.gl_account_no
where O.batch_no = '35117' and G.gl_description NOT IN ('Visa', 'Master Card', 'E Transfer Payments School')
order by O.customer_no desc

It's almost returning the correct things, but there are 7 GL's on each person, even though they didn't all pay towards them, because there are 7 GL's in the batch. I'm almost certain this has something to do with my left and right joins, but I'm not sure how to fix it. Any help with this would be appreciated!! 

Thanks so much! 

Nicki

Parents Reply Children