JeffS23
Posting Yak Master
212 Posts |
Posted - 2007-10-17 : 16:17:52
|
In this question, I have posted two sets of coding .... one from a Stored Procedure that pulls what I want in the Report Query I have that currently works. My client wants to add in the Patient Contact name in the report example I have listed (see My Existing Query:). When I try adding in other ways, I get a row for each contact .... I just want the first one listed (as the SP lists). Any help is appreciated. Part from another Stored Procedure:DECLARE @pPatientProfileId intCREATE TABLE #Contacts_tmp ( id_col Numeric(3,0) identity, PatientProfileId int NULL , First varchar (50) NULL, Middle varchar (50) NULL, Last varchar (50) NULL, Address1 VARCHAR (100)NULL, Address2 VARCHAR (100)NULL, City VARCHAR (60) NULL, State VARCHAR (20) NULL, Zip VARCHAR (20) NULL, Phone1 varchar (15) NULL, Phone1Type varchar (10) NULL, Phone2 varchar (15) NULL, Phone2Type varchar (10) NULL, ContactRelToPatient varchar (20) NULL )INSERT #Contacts_tmpSELECT PatientProfileId, First, Middle, Last, Address1, Address2, City, State, Zip, Phone1, Phone1Type, Phone2, Phone2Type, ContactRelToPatientFROM PatientContacts pcWHERE pc.PatientProfileId = @pPatientProfileId ORDER BY CreatedSELECT 'Contact1First' = IsNull(First,''), 'Contact1Middle' = IsNull(Middle,''), 'Contact1Last' = IsNull(Last,''), 'Contact1Address1' = ISNULL(address1,''), 'Contact1Address2' = ISNULL(address2,''), 'Contact1City' = ISNULL(city,''), 'Contact1State' = ISNULL(state,''), 'Contact1Zip' = ISNULL(zip,''), 'Contact1Phone' = IsNull(Phone1,''), 'Contact1Phone1Type' = IsNull(Phone1Type, ''), 'Contact1Phone2' = IsNull(Phone2, ''), 'Contact1Phone2Type' = IsNull(Phone2Type, ''), 'Contact1RelToPatient' = IsNull(ContactRelToPatient,'')FROM #Contacts_tmpWHERE id_col = 1My Existing Query:/* Guarantor Balance */SET NOCOUNT ONIF ( SELECT OBJECT_ID('tempdb..#A','U') ) IS NOT NULL DROP TABLE #AIF ( SELECT OBJECT_ID('tempdb..#B','U') ) IS NOT NULL DROP TABLE #BIF ( SELECT OBJECT_ID('tempdb..#C','U') ) IS NOT NULL DROP TABLE #CIF ( SELECT OBJECT_ID('tempdb..#GPAll','U') ) IS NOT NULL DROP TABLE #GPAllCREATE TABLE #A(GuarantorId int NOT NULL, TotalInsBalance money NULL, TotalPatBalance money NULL, TotalBalance money NULL)CREATE TABLE #B(GuarantorId int NOT NULL, PatientProfileId int NOT NULL, InsBalance money NULL, PatBalance money NULL, Balance money NULL, Description varchar(255) NULL, PatientVisitId int NULL, VisitInsPayment money NULL, VisitPatPayment money NULL, VisitInsBalance money NULL, VisitPatBalance money NULL, VisitStatus varchar(255) NULL)CREATE TABLE #GPAll( GuarantorId int NOT NULL, PaymentDate datetime NOT NULL, Amount money NULL)CREATE TABLE #C(GuarantorId int NULL, LastPaymentDate datetime NULL, PaymentAge int NULL, Amount money NULL,)INSERT #ASELECT gr.GuarantorId, SUM(ppa.InsBalance) AS TotalInsBalance, SUM(ppa.PatBalance) AS TotalPatBalance, SUM(ppa.Balance) AS TotalBalanceFROM PatientProfile pp INNER JOIN Guarantor gr ON pp.GuarantorId = gr.GuarantorId INNER JOIN PatientProfileAgg ppa ON pp.PatientProfileId = ppa.PatientProfileIdWHERE ( ('43' IS NOT NULL AND gr.GuarantorId IN(43)) OR '43' IS NULL )GROUP BY gr.GuarantorIdORDER BY gr.GuarantorIdINSERT #BSELECT gr.GuarantorId, pp.PatientProfileId, ppa.InsBalance AS InsBalance, ppa.PatBalance AS PatBalance, ppa.Balance AS Balance, pc.Description, pv.PatientVisitId, pva.InsPayment AS VisitInsPayment, pva.PatPayment AS VisitPatPayment, pva.InsBalance AS VisitInsBalance, pva.PatBalance AS VisitPatBalance, bs.Description AS VisitStatusFROM Guarantor gr INNER JOIN PatientProfile pp ON gr.GuarantorId = pp.GuarantorId INNER JOIN PatientProfileAgg ppa ON pp.PatientProfileId = ppa.PatientProfileId INNER JOIN PatientVisit pv ON pp.PatientProfileId = pv.PatientProfileId INNER JOIN PatientVisitAgg pva ON pv.PatientVisitId = pva.PatientVisitId LEFT JOIN Medlists bs on bs.TableName = 'BillStatus' AND pv.BillStatus = bs.JoinId LEFT JOIN PatientCorrespondence pc ON pv.PatientVisitId = pc.PatientVisitId WHERE(( ('43' IS NOT NULL AND gr.GuarantorId IN(43)) OR '43' IS NULL )AND---pva.PatBalance > 0pv.Billstatus NOT IN (12))INSERT #GPAll SELECT px.GuarantorId, px.PaymentDate AS PaymentDate, SUM(px.Amount) AS Amount FROM ( SELECT gr.GuarantorId, b.Entry AS PaymentDate, pm.Amount FROM PaymentMethod pm INNER JOIN Batch b ON pm.BatchId = b.BatchId INNER JOIN patientprofile pp ON pm.PayerId = pp.PatientProfileId INNER JOIN Guarantor gr ON pp.GuarantorId = gr.GuarantorId WHERE pm.PayerType = 'Patient' AND pm.Amount <> 0 AND ( ('43' IS NOT NULL AND gr.GuarantorId IN(43)) OR '43' IS NULL ) UNION ALL SELECT gr.GuarantorId, b.Entry AS PaymentDate, pm.Amount FROM PaymentMethod pm INNER JOIN Batch b ON pm.BatchId = b.BatchId INNER JOIN Guarantor gr ON pm.PayerId = gr.GuarantorId WHERE pm.PayerType = 'Guarantor' AND pm.Amount <> 0 AND ( ('43' IS NOT NULL AND gr.GuarantorId IN(43)) OR '43' IS NULL ) ) AS px GROUP BY GuarantorId, PaymentDate ORDER BY GuarantorIdINSERT #C SELECT gp.GuarantorId, gp.PaymentDate AS LastPaymentDate, DATEDIFF(day, gp.PaymentDate, getdate()) AS PaymentAge, gp.Amount FROM #GPAll gp WHERE gp.PaymentDate = ( SELECT MAX(PaymentDate) FROM #GPAll gpm WHERE gp.GuarantorId = gpm.GuarantorId ) ORDER BY GuarantorIdSELECT dbo.FormatName(gr.Prefix, gr.First, gr.Middle, gr.Last, gr.Suffix) AS GuarantorName, ISNULL(gr.Address1,'')+ ' ' + ISNULL(gr.Address2,'') AS [Guarantor Address], ISNULL(gr.City,'')+ ' ' + ISNULL(gr.State,'')+ ' ' + ISNULL(gr.Zip,'') AS [Guarantor CSZ], LEFT(gr.SSN, 3) + '-' + SUBSTRING(gr.SSN, 4, 2) + '-' + RIGHT(gr.SSN, 4) AS SSN, '(' + LEFT(gr.Phone1, 3) + ') ' + SUBSTRING(gr.Phone1, 4, 3) + '-' + RIGHT(gr.Phone1, 4) AS Phone, '(' + LEFT(gr.Phone2, 3) + ') ' + SUBSTRING(gr.Phone2, 4, 3) + '-' + RIGHT(gr.Phone2, 4) AS Phone2, RefA.TotalInsBalance AS GuarantorInsBalance, RefA.TotalPatBalance AS GuarantorPatBalance, RefA.TotalBalance AS GuarantorBalance, dbo.FormatName(pp.Prefix, pp.First, pp.Middle, pp.Last, pp.Suffix) AS PatientName, RefB.InsBalance, RefB.PatBalance, RefC.LastPaymentDate, RefC.Amount, RefB.Balance, pv.Visit, RefB.Description, pv.TicketNumber, RefB.VisitInsPayment, RefB.VisitPatPayment, RefB.VisitInsBalance, RefB.VisitPatBalance, RefB.VisitStatusFROM #A as RefA INNER JOIN #B as RefB ON RefB.GuarantorId = RefA.GuarantorId LEFT OUTER JOIN #C as RefC ON RefA.GuarantorId = RefC.GuarantorId INNER JOIN Guarantor gr ON gr.GuarantorId = RefA.GuarantorId INNER JOIN PatientProfile pp ON pp.PatientProfileId = RefB.PatientProfileId INNER JOIN PatientVisit pv ON pv.PatientVisitId = RefB.PatientVisitId WHERE ( pv.Visit >= ISNULL(NULL,'1/1/1900') AND pv.Visit < DATEADD(d, 1, ISNULL(NULL,'1/1/3000')) ) AND ( 1 < 2 OR 1 > 3 OR CASE WHEN 1 = 2 THEN RefA.TotalBalance END > NULL OR CASE WHEN 1 = 3 THEN RefA.TotalBalance END < NULL OR (CASE WHEN 1 = 3 THEN ABS(RefB.InsBalance) END < NULL) OR (CASE WHEN 1 = 3 THEN ABS(RefB.PatBalance) END < NULL) ) AND ( ((30 IS NULL) OR (RefC.PaymentAge IS NULL)) OR (RefC.PaymentAge > 30) ) AND --Filter on insurance balance ( (1 = 1) OR (1 = 2 AND RefA.TotalInsBalance > NULL) OR (1 = 3 AND RefA.TotalInsBalance < NULL) ) AND --Filter on patient balance ( (1 = 1) OR (1 = 2 AND RefA.TotalPatBalance > NULL) OR (1 = 3 AND RefA.TotalPatBalance < NULL) )GROUP BY dbo.FormatName(gr.Prefix, gr.First, gr.Middle, gr.Last, gr.Suffix), ISNULL(gr.Address1,'')+ ' ' + ISNULL(gr.Address2,''), ISNULL(gr.City,'')+ ' ' + ISNULL(gr.State,'')+ ' ' + ISNULL(gr.Zip,''), LEFT(gr.SSN, 3) + '-' + SUBSTRING(gr.SSN, 4, 2) + '-' + RIGHT(gr.SSN, 4), '(' + LEFT(gr.Phone1, 3) + ') ' + SUBSTRING(gr.Phone1, 4, 3) + '-' + RIGHT(gr.Phone1, 4), '(' + LEFT(gr.Phone2, 3) + ') ' + SUBSTRING(gr.Phone2, 4, 3) + '-' + RIGHT(gr.Phone2, 4), RefA.TotalInsBalance, RefA.TotalPatBalance, RefA.TotalBalance, dbo.FormatName(pp.Prefix, pp.First, pp.Middle, pp.Last, pp.Suffix), RefB.InsBalance, RefB.PatBalance, RefC.LastPaymentDate, RefC.Amount, RefB.Balance, pv.Visit, RefB.Description, pv.TicketNumber, RefB.VisitInsPayment, RefB.VisitPatPayment, RefB.VisitInsBalance, RefB.VisitPatBalance, RefB.VisitStatusORDER BY GuarantorName, PatientNameDROP TABLE #ADROP TABLE #BDROP TABLE #CDROP TABLE #GPAll |
|