Hello,
I have a report that pulls a patients insurance for each a visit. Each time a patient visits the hospital they get an AccountNumber. A patient could have multiple insurances for that visit. I am trying to get all policy numbers on the same line as the account number, thus decrease the number of lines for each account from 2 or 3 to one. I think I need to use a pivot table to do that but I can't seem to get it to work. Here is what I tried:
SELECT BarVisits.AccountNumber, AdmInsuranceOrder.InsuranceOrderID,
AdmInsuranceOrder.InsuranceID, AdmInsuredInfo.PolicyNumber
FROM BarVisits INNER JOIN
AdmInsuranceOrder ON BarVisits.VisitID = AdmInsuranceOrder.VisitID
INNER JOIN
AdmInsuredInfo ON AdmInsuranceOrder.VisitID = AdmInsuredInfo.VisitID
AND AdmInsuranceOrder.InsuranceID = AdmInsuredInfo.InsuranceID
---------
SELECT AdmInsuredInfo.PolicyNumber, [INS1], [INS2], [INS3]
FROM BarVisits INNER JOIN
AdmInsuranceOrder ON BarVisits.VisitID = AdmInsuranceOrder.VisitID
INNER JOIN
AdmInsuredInfo ON AdmInsuranceOrder.VisitID = AdmInsuredInfo.VisitID
AND AdmInsuranceOrder.InsuranceID = AdmInsuredInfo.InsuranceID
PIVOT
(
MAX(AdmInsuredInfo.PolicyNumber) FOR BarVisits.AccountNumber
IN ([INS1], [INS2], [INS3])
) as pvt
If I run that I get these errors:
Msg 8156, Level 16, State 1, Line 10
The column 'SourceID' was specified multiple times for 'pvt'.
Msg 4104, Level 16, State 1, Line 10
The multi-part identifier "AdmInsuredInfo.PolicyNumber" could not be bound.
Any help appreciated,
Thanks
Evan
I have a report that pulls a patients insurance for each a visit. Each time a patient visits the hospital they get an AccountNumber. A patient could have multiple insurances for that visit. I am trying to get all policy numbers on the same line as the account number, thus decrease the number of lines for each account from 2 or 3 to one. I think I need to use a pivot table to do that but I can't seem to get it to work. Here is what I tried:
SELECT BarVisits.AccountNumber, AdmInsuranceOrder.InsuranceOrderID,
AdmInsuranceOrder.InsuranceID, AdmInsuredInfo.PolicyNumber
FROM BarVisits INNER JOIN
AdmInsuranceOrder ON BarVisits.VisitID = AdmInsuranceOrder.VisitID
INNER JOIN
AdmInsuredInfo ON AdmInsuranceOrder.VisitID = AdmInsuredInfo.VisitID
AND AdmInsuranceOrder.InsuranceID = AdmInsuredInfo.InsuranceID
---------
SELECT AdmInsuredInfo.PolicyNumber, [INS1], [INS2], [INS3]
FROM BarVisits INNER JOIN
AdmInsuranceOrder ON BarVisits.VisitID = AdmInsuranceOrder.VisitID
INNER JOIN
AdmInsuredInfo ON AdmInsuranceOrder.VisitID = AdmInsuredInfo.VisitID
AND AdmInsuranceOrder.InsuranceID = AdmInsuredInfo.InsuranceID
PIVOT
(
MAX(AdmInsuredInfo.PolicyNumber) FOR BarVisits.AccountNumber
IN ([INS1], [INS2], [INS3])
) as pvt
If I run that I get these errors:
Msg 8156, Level 16, State 1, Line 10
The column 'SourceID' was specified multiple times for 'pvt'.
Msg 4104, Level 16, State 1, Line 10
The multi-part identifier "AdmInsuredInfo.PolicyNumber" could not be bound.
Any help appreciated,
Thanks
Evan