SQL 2005 Query Assistance

Jeff S
Jeff S used Ask the Experts™
on
Please note, the SQL is handled dynamically by the SQL server therefore the coding in some of my WHERE clauses will look odd to you, please ignore this as it is not an issue. What I need help with is I tried to use

MIN(medicarepayment.CheckDate) as CheckDate,

and I still get the two records back on my test patient. I am not sure what I am missing.
SET NOCOUNT ON

DECLARE 
	@groupby1 varchar(200), 
	@groupby2 varchar(200)

SET @groupby1 = convert(varchar(200),'None')
SET @groupby2 = convert(varchar(200),'None')

CREATE TABLE #Summary 
	(
	PatientVisitID INT, 
	[Date of Entry] DATETIME,
	Name VARCHAR(100),
	Guarantor VARCHAR(100), 
	GuarantorId INT,
	[Adjustment Type] VARCHAR(75),
	TicketNumber VARCHAR(25),
	[Procedure Code] VARCHAR(10),
	[Adjustment Amount] money,
	DateofServiceFrom DATETIME,
	Notes VARCHAR(255),
	[Financial Class] VARCHAR(200),
	[Insurance Carrier] VARCHAR(60),
	InsuredID VARCHAR(25),
	[Policy Type] VARCHAR(200),
	Facility VARCHAR(60),
	Group1 VARCHAR(200), 
	Group2 VARCHAR(200),
	MedicaidID VARCHAR(25), 
	NoRecord VARCHAR(1)
	)

INSERT INTO #Summary

SELECT     
	pv.PatientVisitID, 
	b.Entry AS [Date Of Entry], 
	pp.[Last] + ', ' + pp.[First] AS Name, 
	dbo.FormatName(g.Prefix , g.First , g.Middle , g.Last , g.Suffix) AS Guarantor,
	ISNULL(g.GuarantorID , 0) AS GuarantorId,
    	MedLists.Description AS [Adjustment Type], 
	pv.TicketNumber, 
	pvp.Code AS [Procedure Code], 
    	td.Amount AS [Adjustment Amount], 
	pvp.DateOfServiceFrom, 
	ISNULL(CONVERT(VARCHAR(255), t.Note), ' ') AS Notes, 
	ISNULL(fc.Description, 'No Financial Class') AS [Financial Class], 
	ISNULL(ic.ListName,'No Insurance') AS [Insurance Carrier], 
	ISNULL(pi.InsuredId , '') AS InsuredID, 
	ISNULL(ml.Description,'No Insurance') AS [Policy Type], 
	dff.ListName as Facility,
	CASE @groupby1
		WHEN 'Facility' THEN dff.ListName
		WHEN 'Financial Class' THEN ISNULL(fc.Description, 'No Financial Class')
		WHEN 'Insurance Carrier' THEN ISNULL(ic.ListName,'No Insurance')
		WHEN 'Policy Type' THEN ISNULL(ml.Description,'No Insurance') 
		ELSE 'None'
	END as Group1,
	CASE @groupby2
		WHEN 'Facility' THEN dff.ListName
		WHEN 'Financial Class' THEN ISNULL(fc.Description, 'No Financial Class')
		WHEN 'Insurance Carrier' THEN ISNULL(ic.ListName,'No Insurance')
		WHEN 'Policy Type' THEN ISNULL(ml.Description,'No Insurance') 
		ELSE 'None'
	END as Group2,
	medicareInsurance.InsuredId AS MedicaidId,
	ISNULL(medicareInsurance.NoRecord,'') AS NoRecord

FROM         
	PaymentMethod pm
	INNER JOIN VisitTransactions vt ON pm.PaymentMethodId = vt.PaymentMethodId 
	INNER JOIN Transactions t ON vt.VisitTransactionsId = t.VisitTransactionsId 
	INNER JOIN TransactionDistributions td ON t.TransactionsId = td.TransactionsId 
	INNER JOIN Batch b ON pm.BatchId = b.BatchId 
	INNER JOIN PatientVisit pv ON vt.PatientVisitid = pv.PatientVisitId 
	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
	LEFT OUTER JOIN MedLists ON t.ActionTypeMId = MedLists.MedListsId 
	LEFT OUTER JOIN PatientVisitProcs pvp ON td.PatientVisitProcsId = pvp.PatientVisitProcsId 
	LEFT OUTER JOIN InsuranceCarriers ic ON pv.PrimaryInsuranceCarriersID = ic.InsuranceCarriersID 
	LEFT JOIN PatientInsurance pi ON pv.PrimaryPICarrierId = pi.PatientInsuranceId 
	LEFT OUTER JOIN MedLists fc ON pv.FinancialClassMID = fc.MedListsID 
	LEFT OUTER JOIN MedLists ml ON ic.PolicyTypeMID = ml.MedListsID
	INNER JOIN DoctorFacility dff ON pv.FacilityId = dff.DoctorFacilityId 
	LEFT OUTER JOIN Guarantor g ON pp.GuarantorID = g.GuarantorID
	LEFT OUTER JOIN
	(SELECT medicarepvi.patientvisitid,medicarepi.InsuredID, NoRecord='X' FROM PatientVisitInsurance medicarepvi INNER JOIN patientinsurance medicarepi ON medicarepvi.PatientInsuranceID = medicarepi.PatientInsuranceID
	WHERE medicarepi.InsuranceCarriersId IN (4,3,7,2)) medicareInsurance ON medicareInsurance.patientvisitid = pv.patientvisitid 

WHERE     
	t.Type = 'A'
	AND --Filter on Source of Adjustment
	(
	('3' = pm.Source)  OR
	('3' = '3')
	)
	AND  --Filter on Carrier
	(
	(NULL IS NOT NULL AND pv.PrimaryInsuranceCarriersID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on adjustment type
	(
	(2 = 2 AND t.ActionTypeMId IN (3444)) OR
	(2 = 1)
	)
	AND  --Filter on doctor
	(
	(NULL IS NOT NULL AND pv.DoctorID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on company
	(
	(NULL IS NOT NULL AND pv.CompanyID IN (NULL)) OR
	(NULL IS NULL)
	)
        	AND  --Filter on date range
	(
	(1 = 1 AND b.Entry >= ISNULL('01/01/2010', '1/1/1900') AND b.Entry < DATEADD(d,1,ISNULL('12/31/2010','1/1/3000'))) OR
	(1 <> 1 AND pm.DepositDate >= ISNULL('01/01/2010', '1/1/1900') AND pm.DepositDate < DATEADD(d,1,ISNULL('12/31/2010','1/1/3000')))
	)
        	AND  --Filter on date range
	(
	(pvp.DateofServiceFrom >= ISNULL(NULL, '1/1/1900') AND pvp.DateofServiceFrom  <  DATEADD(d,1,ISNULL(NULL,'1/1/3000')))
	)
	AND  --Filter on procedures
	(
	(NULL IS NOT NULL AND pvp.ProceduresId IN (NULL)) OR
	(NULL IS NULL)
	)

ORDER BY 
	pp.[Last] + ', ' + pp.[First], 
	pvp.DateOfServiceFrom

IF 2 = 1 AND NULL IS NOT NULL 

INSERT INTO #Summary

SELECT     
	pv.PatientVisitID, 
	b.Entry AS [Date Of Entry], 
	pp.[Last] + ', ' + pp.[First] AS Name, 
	dbo.FormatName(g.Prefix , g.First , g.Middle , g.Last , g.Suffix) AS Guarantor,
	ISNULL(g.GuarantorID , 0) AS GuarantorId,
    	'FQHC Reimbursement' AS [Adjustment Type], 
	pv.TicketNumber, 
	pvp.Code AS [Procedure Code], 
    	-1 * pvp.TotalFee AS [Adjustment Amount], 
	pvp.DateOfServiceFrom, 
	' ' AS Notes, 
	ISNULL(fc.Description, 'No Financial Class') AS [Financial Class], 
	ISNULL(ic.ListName,'No Insurance') AS [Insurance Carrier], 
	ISNULL(pi.InsuredId , '') AS InsuredID, 
	ISNULL(ml.Description,'No Insurance') AS [Policy Type], 
	dff.ListName as Facility,
	CASE @groupby1
		WHEN 'Facility' THEN dff.ListName
		WHEN 'Financial Class' THEN ISNULL(fc.Description, 'No Financial Class')
		WHEN 'Insurance Carrier' THEN ISNULL(ic.ListName,'No Insurance')
		WHEN 'Policy Type' THEN ISNULL(ml.Description,'No Insurance') 
		ELSE 'None'
	END as Group1,
	CASE @groupby2
		WHEN 'Facility' THEN dff.ListName
		WHEN 'Financial Class' THEN ISNULL(fc.Description, 'No Financial Class')
		WHEN 'Insurance Carrier' THEN ISNULL(ic.ListName,'No Insurance')
		WHEN 'Policy Type' THEN ISNULL(ml.Description,'No Insurance') 
		ELSE 'None'
	END as Group2,
	medicareInsurance.InsuredId AS MedicaidId,
	ISNULL(medicareInsurance.NoRecord,'') AS NoRecord

FROM         
	PatientVisitProcs pvp 
	INNER JOIN PatientVisit pv ON pvp.PatientVisitID = pv.PatientVisitID 
	INNER JOIN Batch b ON pvp.BatchId = b.BatchId 
	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
	LEFT OUTER JOIN InsuranceCarriers ic ON pv.PrimaryInsuranceCarriersID = ic.InsuranceCarriersID 
	LEFT JOIN PatientInsurance pi ON pv.PrimaryPICarrierId = pi.PatientInsuranceId 
	LEFT OUTER JOIN MedLists ml ON ic.PolicyTypeMID = ml.MedListsID 
	LEFT OUTER JOIN MedLists fc ON pv.FinancialClassMID = fc.MedListsID
	INNER JOIN DoctorFacility dff ON pv.FacilityId = dff.DoctorFacilityId 
	LEFT OUTER JOIN Guarantor g ON pp.GuarantorID = g.GuarantorID
	LEFT OUTER JOIN
	(SELECT medicarepvi.patientvisitid,medicarepi.InsuredID, NoRecord='X' FROM PatientVisitInsurance medicarepvi INNER JOIN patientinsurance medicarepi ON medicarepvi.PatientInsuranceID = medicarepi.PatientInsuranceID
	WHERE medicarepi.InsuranceCarriersId IN (4,3,7,2)) medicareInsurance ON medicareInsurance.patientvisitid = pv.patientvisitid 

WHERE   
	--Filter on doctor
	(
	(NULL IS NOT NULL AND pv.DoctorID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Carrier
	(
	(NULL IS NOT NULL AND pv.PrimaryInsuranceCarriersID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on company
	(
	(NULL IS NOT NULL AND pv.CompanyID IN (NULL)) OR
	(NULL IS NULL)
	)
        	AND  --Filter on date range
	(
	(b.Entry >= ISNULL('01/01/2010', '1/1/1900') AND b.Entry < DATEADD(d,1,ISNULL('12/31/2010','1/1/3000'))) 
	)
	AND  --Filter on FQHC procedures
	(
	(NULL IS NOT NULL AND pvp.ProceduresId IN (NULL)) OR
	(NULL IS NULL)
	)

ORDER BY 
	pp.[Last] + ', ' + pp.[First], 
	pvp.DateOfServiceFrom

SELECT 
	s.PatientVisitID, 
	s.[Date of Entry] AS [Date of Entry],
	s.[Name],
	s.Guarantor,
	s.GuarantorId,
	s.[Adjustment Type],
	s.TicketNumber,
	s.[Procedure Code],
	s.[Adjustment Amount],
	s.DateofServiceFrom,
	s.Notes,
	s.[Financial Class],
	s.[Insurance Carrier],
	s.InsuredID,
	s.[Policy Type],
	s.Facility,
	s.Group1,
	s.Group2,
	s.MedicaidID,
	s.NoRecord,
	medicarepayment.amount AS MedicarePayment, 
	MIN(medicarepayment.CheckDate) as CheckDate,
	(
	SELECT min(esf.filetransmitted)
	FROM EDIStatement es 
	INNER JOIN EDIStatementFile esf ON es.EDIStatementFileId = esf.EDIStatementFileId
	WHERE es.GuarantorID = s.GuarantorId and esf.FileTransmitted > medicarepayment.checkdate
	) AS StatementDate

FROM 
	#Summary s 
	INNER JOIN dbo.Guarantor g ON s.GuarantorId = g.GuarantorId 
	LEFT OUTER JOIN
	(
	SELECT 
		COALESCE(pm.CheckDate, pm.DateofEntry) AS Checkdate,
		pm.DateOfEntry AS [Date Of Entry],
		pv.PatientVisitID, 
		pv.TicketNumber,
--		pvp.Code,
--		CASE WHEN pm.Source = 2 THEN SUM(td.Amount)
--		     ELSE 0.0000
--		END AS Amount,
		SUM(td.Amount) AS Amount
--		pvp.DateOfServiceFrom
	FROM    
		PaymentMethod pm
		INNER JOIN VisitTransactions vt ON pm.PaymentMethodId = vt.PaymentMethodId
		INNER JOIN Transactions tc ON vt.VisitTransactionsId = tc.VisitTransactionsId
		INNER JOIN TransactionDistributions td ON tc.TransactionsId = td.TransactionsId
		INNER JOIN PatientVisit pv ON vt.PatientVisitid = pv.PatientVisitId
		INNER JOIN MedLists ml ON tc.ActionTypeMId = ml.MedListsId
--		LEFT JOIN PatientVisitProcs pvp ON td.PatientVisitProcsId = pvp.PatientVisitProcsId
		INNER JOIN InsuranceCarriers ic ON vt.InsuranceCarriersId = ic.InsuranceCarriersId 
		LEFT OUTER JOIN MedLists ml2 ON ic.PolicyTypeMID = ml2.MedListsID
	WHERE   
		tc.Type = 'P'
--		AND td.Amount <> 0.00
		AND tc.Amount > 0
		AND (ml2.Description LIKE 'Medicare%' OR ml2.Description LIKE 'Medicare Primary')
	
	GROUP BY 
		pm.CheckDate, pm.DateofEntry,pv.PatientVisitID, pv.TicketNumber, /*pvp.Code,*/pm.Source--, pvp.DateOfServiceFrom
--	ORDER BY pm.DateofEntry 
	) medicarepayment ON s.ticketnumber = medicarepayment.ticketnumber AND s.PatientVisitID =  medicarepayment.PatientVisitID /*AND s.[Procedure Code] = medicarepayment.code*/ 	
	
GROUP BY 
	s.PatientVisitID, 
	s.[Date Of Entry], 
	s.[Name],
	s.Guarantor,
	s.GuarantorId,
	s.[Adjustment Type],
	s.TicketNumber,
	s.[Procedure Code],
	s.[Adjustment Amount],
	s.DateofServiceFrom,
	s.Notes,
	s.[Financial Class],
	s.[Insurance Carrier],
	s.InsuredID,
	s.[Policy Type],
	s.Facility,
	s.Group1,
	s.Group2,
	s.MedicaidID,
	s.NoRecord,
	medicarepayment.amount,
	medicarepayment.CheckDate
		 
ORDER BY s.Name, medicarepayment.CheckDate

DROP TABLE #Summary

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
because you've 2 different SQLs.
Consultant Software Engineer - .NET Architect
Commented:
If you are using Min(medicarepayment.CheckDate), then you should not use medicarepayment.CheckDate
in group by

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial