Follow Us:

Call us:  (306) 986-8888

Microsoft Access Union Query from Hell. Skill Level: Demi-God

Microsoft Access Union Query from Hell. Skill Level: Demi-God

The worst query I ever had to write which powers a master report for the database user.  Here it is.  I don’t think I can make it smaller, there were too many weird rules, aggregates, and translations in it.  And we needed to pull different columns based on criteria.  And it is a Union Query.  This one makes me tired just reading it.  But it is an excellent example of many different skills in one powerful query….sum, iif, group by, inner join, format, and more.  Skilllevel required is Access Demi-God:

SELECT tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, Sum(tblClaimItemCounts.acresCounted) AS SumOfacresCounted, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.Pickup, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([columnt]<5,” < 5%”,IIf([columnt]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))) AS FinalTotal, qryPriorLoss.FinalTotal AS FinalTotalPrior, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([columnt]<5,”YES”,IIf([columnt]>=90,”YES”,”NO”))))) AS Split, tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed
FROM (((tblPolicies INNER JOIN tblClaims ON tblPolicies.PolicyID = tblClaims.PolicyID) INNER JOIN tblPolicyItems ON tblPolicies.PolicyID = tblPolicyItems.PolicyID) INNER JOIN (tblClaimItems LEFT JOIN tblClaimItemCounts ON tblClaimItems.ClaimItemID = tblClaimItemCounts.ClaimItemID) ON tblPolicyItems.PolicyItemID = tblClaimItems.PolicyItemID) LEFT JOIN qryPriorLoss ON tblPolicyItems.PolicyItemID = qryPriorLoss.PolicyItemNumber
GROUP BY tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([columnt]<5,” < 5%”,IIf([columnt]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))), qryPriorLoss.FinalTotal, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([columnt]<5,”YES”,IIf([columnt]>=90,”YES”,”NO”))))), tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed, tblClaimItems.Pickup
HAVING (((tblPolicyItems.CropID)=1 Or (tblPolicyItems.CropID)=5) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Pickup)=True)) OR (((tblPolicyItems.CropID)=1 Or (tblPolicyItems.CropID)=5) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Claimed)=True))
union
SELECT tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, Sum(tblClaimItemCounts.acresCounted) AS SumOfacresCounted, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.Pickup, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNH]<5,” < 5%”,IIf([COLUMNH]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))) AS FinalTotal, qryPriorLoss.FinalTotal AS FinalTotalPrior, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNH]<5,”YES”,IIf([COLUMNH]>=90,”YES”,”NO”))))) AS Split, tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed
FROM (((tblPolicies INNER JOIN tblClaims ON tblPolicies.PolicyID = tblClaims.PolicyID) INNER JOIN tblPolicyItems ON tblPolicies.PolicyID = tblPolicyItems.PolicyID) INNER JOIN (tblClaimItems LEFT JOIN tblClaimItemCounts ON tblClaimItems.ClaimItemID = tblClaimItemCounts.ClaimItemID) ON tblPolicyItems.PolicyItemID = tblClaimItems.PolicyItemID) LEFT JOIN qryPriorLoss ON tblPolicyItems.PolicyItemID = qryPriorLoss.PolicyItemNumber
GROUP BY tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNH]<5,” < 5%”,IIf([COLUMNH]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))), qryPriorLoss.FinalTotal, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNH]<5,”YES”,IIf([COLUMNH]>=90,”YES”,”NO”))))), tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed, tblClaimItems.Pickup
HAVING (((tblPolicyItems.CropID)=2 Or (tblPolicyItems.CropID)=6 Or (tblPolicyItems.CropID)=7 Or (tblPolicyItems.CropID)=8 Or (tblPolicyItems.CropID)=9 Or (tblPolicyItems.CropID)=10 Or (tblPolicyItems.CropID)=11 Or (tblPolicyItems.CropID)=12 Or (tblPolicyItems.CropID)=61 Or (tblPolicyItems.CropID)=76 Or (tblPolicyItems.CropID)=63) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Pickup)=True)) OR (((tblPolicyItems.CropID)=2 Or (tblPolicyItems.CropID)=6 Or (tblPolicyItems.CropID)=7 Or (tblPolicyItems.CropID)=8 Or (tblPolicyItems.CropID)=9 Or (tblPolicyItems.CropID)=10 Or (tblPolicyItems.CropID)=11 Or (tblPolicyItems.CropID)=12 Or (tblPolicyItems.CropID)=61 Or (tblPolicyItems.CropID)=76 Or (tblPolicyItems.CropID)=63) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Claimed)=True))
UNION
SELECT tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, Sum(tblClaimItemCounts.acresCounted) AS SumOfacresCounted, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.Pickup, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNL]<5,” < 5%”,IIf([COLUMNL]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))) AS FinalTotal, qryPriorLoss.FinalTotal AS FinalTotalPrior, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNL]<5,”YES”,IIf([COLUMNL]>=90,”YES”,”NO”))))) AS Split, tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed
FROM (((tblPolicies INNER JOIN tblClaims ON tblPolicies.PolicyID = tblClaims.PolicyID) INNER JOIN tblPolicyItems ON tblPolicies.PolicyID = tblPolicyItems.PolicyID) INNER JOIN (tblClaimItems LEFT JOIN tblClaimItemCounts ON tblClaimItems.ClaimItemID = tblClaimItemCounts.ClaimItemID) ON tblPolicyItems.PolicyItemID = tblClaimItems.PolicyItemID) LEFT JOIN qryPriorLoss ON tblPolicyItems.PolicyItemID = qryPriorLoss.PolicyItemNumber
GROUP BY tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNL]<5,” < 5%”,IIf([COLUMNL]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))), qryPriorLoss.FinalTotal, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNL]<5,”YES”,IIf([COLUMNL]>=90,”YES”,”NO”))))), tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed, tblClaimItems.Pickup
HAVING (((tblPolicyItems.CropID)=3 Or (tblPolicyItems.CropID)=14 Or (tblPolicyItems.CropID)=16) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Pickup)=True)) OR (((tblPolicyItems.CropID)=3 Or (tblPolicyItems.CropID)=14 Or (tblPolicyItems.CropID)=16) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Claimed)=True))
UNION
SELECT tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, Sum(tblClaimItemCounts.acresCounted) AS SumOfacresCounted, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.Pickup, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNO]<5,” < 5%”,IIf([COLUMNO]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))) AS FinalTotal, qryPriorLoss.FinalTotal AS FinalTotalPrior, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNO]<5,”YES”,IIf([COLUMNO]>=90,”YES”,”NO”))))) AS Split, tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed
FROM (((tblPolicies INNER JOIN tblClaims ON tblPolicies.PolicyID = tblClaims.PolicyID) INNER JOIN tblPolicyItems ON tblPolicies.PolicyID = tblPolicyItems.PolicyID) INNER JOIN (tblClaimItems LEFT JOIN tblClaimItemCounts ON tblClaimItems.ClaimItemID = tblClaimItemCounts.ClaimItemID) ON tblPolicyItems.PolicyItemID = tblClaimItems.PolicyItemID) LEFT JOIN qryPriorLoss ON tblPolicyItems.PolicyItemID = qryPriorLoss.PolicyItemNumber
GROUP BY tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNO]<5,” < 5%”,IIf([COLUMNO]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))), qryPriorLoss.FinalTotal, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNO]<5,”YES”,IIf([COLUMNO]>=90,”YES”,”NO”))))), tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed, tblClaimItems.Pickup
HAVING (((tblPolicyItems.CropID)=4 Or (tblPolicyItems.CropID)=41) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Pickup)=True)) OR (((tblPolicyItems.CropID)=4 Or (tblPolicyItems.CropID)=41) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Claimed)=True))
UNION
SELECT tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, Sum(tblClaimItemCounts.acresCounted) AS SumOfacresCounted, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.Pickup, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNE]<5,” < 5%”,IIf([COLUMNE]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))) AS FinalTotal, qryPriorLoss.FinalTotal AS FinalTotalPrior, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNE]<5,”YES”,IIf([COLUMNE]>=90,”YES”,”NO”))))) AS Split, tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed
FROM (((tblPolicies INNER JOIN tblClaims ON tblPolicies.PolicyID = tblClaims.PolicyID) INNER JOIN tblPolicyItems ON tblPolicies.PolicyID = tblPolicyItems.PolicyID) INNER JOIN (tblClaimItems LEFT JOIN tblClaimItemCounts ON tblClaimItems.ClaimItemID = tblClaimItemCounts.ClaimItemID) ON tblPolicyItems.PolicyItemID = tblClaimItems.PolicyItemID) LEFT JOIN qryPriorLoss ON tblPolicyItems.PolicyItemID = qryPriorLoss.PolicyItemNumber
GROUP BY tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNE]<5,” < 5%”,IIf([COLUMNE]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))), qryPriorLoss.FinalTotal, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNE]<5,”YES”,IIf([COLUMNE]>=90,”YES”,”NO”))))), tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed, tblClaimItems.Pickup
HAVING (((tblPolicyItems.CropID)=13) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Pickup)=True)) OR (((tblPolicyItems.CropID)=13) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Claimed)=True))
UNION
SELECT tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, Sum(tblClaimItemCounts.acresCounted) AS SumOfacresCounted, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.Pickup, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNF]<5,” < 5%”,IIf([COLUMNF]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))) AS FinalTotal, qryPriorLoss.FinalTotal AS FinalTotalPrior, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNF]<5,”YES”,IIf([COLUMNF]>=90,”YES”,”NO”))))) AS Split, tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed
FROM (((tblPolicies INNER JOIN tblClaims ON tblPolicies.PolicyID = tblClaims.PolicyID) INNER JOIN tblPolicyItems ON tblPolicies.PolicyID = tblPolicyItems.PolicyID) INNER JOIN (tblClaimItems LEFT JOIN tblClaimItemCounts ON tblClaimItems.ClaimItemID = tblClaimItemCounts.ClaimItemID) ON tblPolicyItems.PolicyItemID = tblClaimItems.PolicyItemID) LEFT JOIN qryPriorLoss ON tblPolicyItems.PolicyItemID = qryPriorLoss.PolicyItemNumber
GROUP BY tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNF]<5,” < 5%”,IIf([COLUMNF]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))), qryPriorLoss.FinalTotal, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNF]<5,”YES”,IIf([COLUMNF]>=90,”YES”,”NO”))))), tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed, tblClaimItems.Pickup
HAVING (((tblPolicyItems.CropID)=18) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Pickup)=True)) OR (((tblPolicyItems.CropID)=18) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Claimed)=True))
UNION SELECT tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, Sum(tblClaimItemCounts.acresCounted) AS SumOfacresCounted, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.Pickup, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNV]<5,” < 5%”,IIf([COLUMNV]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))) AS FinalTotal, qryPriorLoss.FinalTotal AS FinalTotalPrior, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNV]<5,”YES”,IIf([COLUMNV]>=90,”YES”,”NO”))))) AS Split, tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed
FROM (((tblPolicies INNER JOIN tblClaims ON tblPolicies.PolicyID = tblClaims.PolicyID) INNER JOIN tblPolicyItems ON tblPolicies.PolicyID = tblPolicyItems.PolicyID) INNER JOIN (tblClaimItems LEFT JOIN tblClaimItemCounts ON tblClaimItems.ClaimItemID = tblClaimItemCounts.ClaimItemID) ON tblPolicyItems.PolicyItemID = tblClaimItems.PolicyItemID) LEFT JOIN qryPriorLoss ON tblPolicyItems.PolicyItemID = qryPriorLoss.PolicyItemNumber
GROUP BY tblPolicyItems.CropID, tblPolicyItems.PolicyItemNumber, tblPolicyItems.ActualAcres, tblClaimItems.TypeOfCrop, tblPolicyItems.Quarter, tblPolicyItems.Section, tblPolicyItems.Township, tblPolicyItems.Range, tblPolicyItems.Meridian, tblClaimItems.ClaimNumber, tblClaims.ClaimID, tblClaimItems.ClaimItemID, tblClaims.PolicyID, tblClaimItems.PolicyItemID, tblPolicies.PolicyNum, tblClaimItems.MatchTotalLoss, tblPolicyItems.InsurancePerAcre, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),[tblclaimitems].[status],IIf([matchtotalloss]>0,[matchtotalloss],(IIf([COLUMNV]<5,” < 5%”,IIf([COLUMNV]>=90,”100%”,Format([TBLCLAIMITEMS].[AgreedLoss],”#,##0.0″)))))), qryPriorLoss.FinalTotal, qryPriorLoss.SumOfAgreedThisLoss, IIf(([tblclaimitems].[status]=”defer”) Or ([tblclaimitems].[status]=”release”) Or ([tblclaimitems].[status]=”incomplete”),”NO”,IIf([matchtotalloss]>0,”NO”,(IIf([COLUMNV]<5,”YES”,IIf([COLUMNV]>=90,”YES”,”NO”))))), tblClaimItems.Pickup, tblClaimItems.Nonwaivered, tblClaimItems.Claimed, tblClaimItems.Pickup
HAVING (((tblPolicyItems.CropID)=17 Or ((tblPolicyItems.CropID) Between 19 And 40) Or ((tblPolicyItems.CropID) Between 42 And 60) Or ((tblPolicyItems.CropID) Between 62 And 75)) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Pickup)=True)) OR (((tblPolicyItems.CropID)=17 Or (tblPolicyItems.CropID)=77 OR ((tblPolicyItems.CropID) Between 19 And 40) Or ((tblPolicyItems.CropID) Between 42 And 60) Or ((tblPolicyItems.CropID) Between 62 And 75)) AND ((tblClaims.ClaimID)=[tempvars]![tmpclaimid]) AND ((tblClaimItems.Claimed)=True));
Jeff Shirley