Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with Union Subquery

Author  Topic 

em172967
Starting Member

10 Posts

Posted - 2014-07-10 : 14:20:04
Error: Multi-part identifier "...." could not be bound

SELECT        payer_mstr.payer_name, CAST(YEAR(charges.zero_bal_date) AS char(4)) + '/' + CAST(MONTH(charges.zero_bal_date) AS varchar(2)) AS [YYYY/MM], 
CASE WHEN t_type = 'CH' THEN myunion.charges ELSE 0 END AS CHARGES, CASE WHEN t_type = 'A' THEN t_amt ELSE 0 END AS ADJUSTMENTS,
CASE WHEN t_type = 'C' THEN t_amt ELSE 0 END AS PAYMENTS, CASE WHEN t_type = 'R' THEN t_amt ELSE 0 END AS REFUNDS
FROM charges INNER JOIN
payer_mstr INNER JOIN
transactions ON payer_mstr.payer_id = transactions.payer_id INNER JOIN
patient_encounter ON transactions.person_id = patient_encounter.person_id AND transactions.source_id = patient_encounter.enc_id ON
charges.source_id = patient_encounter.enc_id AND charges.person_id = patient_encounter.person_id AND charges.source_id = transactions.source_id INNER JOIN
(SELECT charges_2.source_id, charges_2.amt AS CHARGES, 'CH' AS t_type, 0 AS t_amt
FROM charges AS charges_2 INNER JOIN
payer_mstr AS payer_mstr_2 INNER JOIN
transactions AS transactions_2 ON payer_mstr_2.payer_id = transactions_2.payer_id INNER JOIN
patient_encounter AS patient_encounter_2 ON transactions_2.person_id = patient_encounter_2.person_id AND
transactions_2.source_id = patient_encounter_2.enc_id ON charges_2.source_id = patient_encounter_2.enc_id AND
charges_2.person_id = patient_encounter_2.person_id AND charges_2.source_id = transactions_2.source_id
UNION
SELECT charges.source_id, 0 AS CHARGES, transactions.type AS t_type, transactions.tran_amt AS t_amt
FROM charges AS charges_1 INNER JOIN
payer_mstr AS payer_mstr_1 INNER JOIN
transactions AS transactions_1 ON payer_mstr_1.payer_id = transactions_1.payer_id INNER JOIN
patient_encounter AS patient_encounter_1 ON transactions_1.person_id = patient_encounter_1.person_id AND
transactions_1.source_id = patient_encounter_1.enc_id ON charges_1.source_id = patient_encounter_1.enc_id AND
charges_1.person_id = patient_encounter_1.person_id AND charges_1.source_id = transactions_1.source_id) AS myunion ON
charges.source_id = myunion.source_id
WHERE (CAST(YEAR(charges.zero_bal_date) AS char(4)) + '/' + CAST(MONTH(charges.zero_bal_date) AS varchar(2)) > '2013/07')

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-07-10 : 14:47:32
It appears to be an alias issue:
SELECT        charges.source_id, 0 AS CHARGES, transactions.type AS t_type, transactions.tran_amt AS t_amt
FROM charges AS charges_1 INNER JOIN
payer_mstr AS payer_mstr_1 INNER JOIN
transactions AS transactions_1 ON payer_mstr_1.payer_id = transactions_1.payer_id INNER JOIN
patient_encounter AS patient_encounter_1 ON transactions_1.person_id = patient_encounter_1.person_id AND
transactions_1.source_id = patient_encounter_1.enc_id ON charges_1.source_id = patient_encounter_1.enc_id AND
charges_1.person_id = patient_encounter_1.person_id AND charges_1.source_id = transactions_1.source_id) AS myunion ON
Go to Top of Page
   

- Advertisement -