I have 3 tables.
One is a visit table. Visits have charges connected with them.
The second is a charges table and one is a transactions table. Transactions are always connected to charges.
The transactions table has payments and adjustments in two separate fields, in separate records.
There can be an adjustment and a payment for any one charge. (charges are positive figures and payments and adjustments can be positive or negative figures depending on whether they are being reversed or not)
I want to exclude any transactions on a charge if there is a payment on it. And I want to include adjustments greater than zero if there are no payments or a payment of zero.
I tried joining them all together and putting adjustments > 0 and payments < 0 but I get no records returned because they are on separate line items (records).
So then I tried a union query but it treats them separately and I can eliminate payments but still get the adjustments on the charge.
example:
Visit 1
Charge 100.00, adjustment 50.00, payment -40.00
(I don't want to see any of those because there is a payment)
Visit 2
Charge 150.00, adjustment 55.00, payment 0.00
I want to see this one listed because there is a zero payment.
Visit 3
Charge 125.00, adjustment 10.00
I want to see this one because there is no payment
SELECT visit.enc_nbr,
charges.service_item_id,
charges.amt,
trans_detail.paid_amt,
trans_detail.adj_amt,
payer_mstr.payer_name,
payer_mstr.payer_id,
tran_code_mstr.type,
transactions.trans_id,
tran_code_mstr.tran_code_id,
charges.charge_id,
transactions.type
FROM
NGProd.dbo.transactions transactions
INNER JOIN NGProd.dbo.visit visit ON transactions.source_id=visit.enc_id
INNER JOIN NGProd.dbo.tran_code_mstr tran_code_mstr ON transactions.tran_code_id=tran_code_mstr.tran_code_id
INNER JOIN NGProd.dbo.trans_detail trans_detail ON transactions.trans_id=trans_detail.trans_id
LEFT OUTER JOIN NGProd.dbo.person_payer person_payer ON visit.cob1_person_payer_id=person_payer.person_payer_id
INNER JOIN NGProd.dbo.payer_mstr payer_mstr ON person_payer.payer_id=payer_mstr.payer_id
LEFT OUTER JOIN NGProd.dbo.charges charges ON trans_detail.charge_id=charges.charge_id
WHERE payer_mstr.payer_id='1139ECCF-2662-4139-89C7-2AA65789032C'
AND trans_detail.adj_amt>0
AND tran_code_mstr.tran_code_id<>'7F50E9A1-5B3D-4BDB-8D53-67490629AFC8'
Thank you...
One is a visit table. Visits have charges connected with them.
The second is a charges table and one is a transactions table. Transactions are always connected to charges.
The transactions table has payments and adjustments in two separate fields, in separate records.
There can be an adjustment and a payment for any one charge. (charges are positive figures and payments and adjustments can be positive or negative figures depending on whether they are being reversed or not)
I want to exclude any transactions on a charge if there is a payment on it. And I want to include adjustments greater than zero if there are no payments or a payment of zero.
I tried joining them all together and putting adjustments > 0 and payments < 0 but I get no records returned because they are on separate line items (records).
So then I tried a union query but it treats them separately and I can eliminate payments but still get the adjustments on the charge.
example:
Visit 1
Charge 100.00, adjustment 50.00, payment -40.00
(I don't want to see any of those because there is a payment)
Visit 2
Charge 150.00, adjustment 55.00, payment 0.00
I want to see this one listed because there is a zero payment.
Visit 3
Charge 125.00, adjustment 10.00
I want to see this one because there is no payment
SELECT visit.enc_nbr,
charges.service_item_id,
charges.amt,
trans_detail.paid_amt,
trans_detail.adj_amt,
payer_mstr.payer_name,
payer_mstr.payer_id,
tran_code_mstr.type,
transactions.trans_id,
tran_code_mstr.tran_code_id,
charges.charge_id,
transactions.type
FROM
NGProd.dbo.transactions transactions
INNER JOIN NGProd.dbo.visit visit ON transactions.source_id=visit.enc_id
INNER JOIN NGProd.dbo.tran_code_mstr tran_code_mstr ON transactions.tran_code_id=tran_code_mstr.tran_code_id
INNER JOIN NGProd.dbo.trans_detail trans_detail ON transactions.trans_id=trans_detail.trans_id
LEFT OUTER JOIN NGProd.dbo.person_payer person_payer ON visit.cob1_person_payer_id=person_payer.person_payer_id
INNER JOIN NGProd.dbo.payer_mstr payer_mstr ON person_payer.payer_id=payer_mstr.payer_id
LEFT OUTER JOIN NGProd.dbo.charges charges ON trans_detail.charge_id=charges.charge_id
WHERE payer_mstr.payer_id='1139ECCF-2662-4139-89C7-2AA65789032C'
AND trans_detail.adj_amt>0
AND tran_code_mstr.tran_code_id<>'7F50E9A1-5B3D-4BDB-8D53-67490629AFC8'
Thank you...