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
  • ,

    Given the above, I assume you are looking for the GL accounts for the actual PAYMENTS that were processed and not the GLs that were attached to the ticketing and fee layers attached to the price types on your performances.  If I have misunderstood, let me know.  But, assuming you want the GLs attached to the payments that your box office/website took, what you are going to want to do is something like this (below).  The problem with just linking the batch to the posting history table is that you definitely WILL get every single GL that was in that batch returned for every customer.  When it comes to payments, the best way is really just to go to the payment table to get started.  Then, when you finally link to the posting history table, you need to confirm that you are linking BOTH the post number AND the asset GL number from the associated payment method that was taken.

    SELECT DISTINCT	p.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,
    				gp.post_no,
    				gp.gl_act_no,
    				g.gl_description
    FROM		T_PAYMENT p WITH (NOLOCK)
    	JOIN	TR_PAYMENT_METHOD pm WITH (NOLOCK) ON p.pmt_method = pm.id
    	JOIN	T_BATCH b WITH (NOLOCK) ON p.batch_no = b.batch_no
    	JOIN	T_GL_POSTING_HISTORY gp WITH (NOLOCK) ON b.post_no = gp.post_no AND pm.asset_gl_no = gp.gl_act_no
    	JOIN	T_CUSTOMER c WITH (NOLOCK) ON p.customer_no = c.customer_no
    	JOIN	T_ORDER o WITH (NOLOCK) ON c.customer_no = o.customer_no AND b.batch_no = o.batch_no
    	JOIN	T_GL_ACCOUNT g WITH (NOLOCK) ON gp.gl_act_no = g.gl_account_no
    WHERE	p.batch_no = 35117
    	AND	g.gl_description NOT IN ('Visa','Master Card','E Transfer Payments School')
    ORDER BY	p.customer_no
    

    Let me know if that does not help you get where you need to go.

    John A. Moskal II

  • Hi John!

    Thanks for this… I tried to run it and it returned a big fat nothing. And there are definitely payments on that batch. Any ideas why?

    Thanks!!

Reply Children