I have edited the SQL to use DISTINCT however I am still getting the same data as when I use GROUP BY. Any suggestions? When I use GROUP BY it requires that all the fields in the select clause are included, can anyone tell me why that is?
Any help is greatly appreciated.
SELECT DISTINCT
TrnClaim.ClientID
, TrnClaim.FileNo
, TrnLegend.RecordNo
, RTRIM(dbo.TrnClient.ClientName + ' ' + ISNULL(dbo.TrnClient.Initials,'') + ' ' + (CASE WHEN dbo.TrnClient.LanguageCode = 'A' THEN ISNULL(dbo.TrnClient.Title, 'Mr') ELSE ISNULL(dbo.TrnClient.Title,'')END)) AS NameInitials
, TrnClient.IDNumber
, ISNULL(dbo.MstInsurer.InsurerName, 'Unknown') AS InsurerName
, CAST(dbo.TrnPolicy.AgentID AS VARCHAR(10)) + ' ' + ISNULL(CAST(dbo.MstAgent.AgentName AS VARCHAR(50)),'Unknown') AS AgentIDName
, CAST(dbo.TrnPolicy.TeamID AS VARCHAR(10)) + ' ' + ISNULL(dbo.VieTeamSelect.TeamName, 'Unknown') AS TeamIDName
, CAST(dbo.TrnPolicy.LinkID AS VARCHAR(10)) + ' ' + ISNULL(dbo.MstLink.LinkName, 'Unknown') AS LinkIDName
, CAST(dbo.TrnPolicy.SubagentID AS VARCHAR(10)) + ' ' + ISNULL(dbo.MstSubagent.SubagentName, 'Unknown') AS SubagentIDName
, CAST(dbo.TrnPolicy.MarketerID AS VARCHAR(10)) + ' ' + ISNULL(dbo.MstMarketerCodes.MarketerName, 'Unknown') AS MarketerIDName
, dbo.TrnRisk.RiskID
, dbo.TrnRisk.Registration
, dbo.TrnRisk.VehID
, dbo.TrnRisk.VinNo
, dbo.TrnRisk.VehMake
, dbo.TrnRisk.VehMake
, dbo.TrnRisk.VehModel
, dbo.TrnRisk.VehModel
, dbo.TrnExt.ExtStartDate
, dbo.TrnExt.ExtEndDate
, dbo.MstRateCodes.RateCodeID
, dbo.TrnExt.ExtDesc
, dbo.TrnPolicy.Status AS PolicyStatus
, dbo.TrnPolicy.PayMeth
, dbo.TrnPolicy.PolicyPeriod
, 1 AS [Count]
, dbo.TrnPolicy.TotalPrem AS LastMonthPremium
--, LastPolicy.TotalDue AS LastMonthTotalDue
, dbo.TrnPolicy.TeamID
, dbo.TrnPolicy.AgentID
, dbo.TrnPolicy.LinkID
, ISNULL(dbo.TrnPolicy.LeadInsurerID,MstInsurer.InsurerID) AS InsurerIDForFilter
, dbo.TrnLegend.LegendDescr
, dbo.TrnLegend.ActiveDate
FROM dbo.TrnClaim
INNER JOIN dbo.TrnPolicy ON
TrnClaim.ClientID = dbo.TrnPolicy.ClientID
AND
TrnClaim.FileNo = dbo.TrnPolicy.FileNo
INNER JOIN dbo.TrnClient ON
dbo.TrnPolicy.ClientID = dbo.TrnClient.ClientID
INNER JOIN dbo.TrnRisk ON
dbo.TrnPolicy.ClientID = dbo.TrnRisk.ClientID
AND
dbo.TrnPolicy.FileNo = dbo.TrnRisk.FileNo
INNER JOIN dbo.TrnExt ON
dbo.TrnRisk.ClientID = dbo.TrnExt.ClientID
AND
dbo.TrnRisk.FileNo = dbo.TrnExt.FileNo
LEFT OUTER JOIN dbo.MstRateCodes ON
dbo.MstRateCodes.RateCodeID = dbo.TrnExt.ExtCode
LEFT OUTER JOIN dbo.MstInsurer MstInsurerLead ON
dbo.TrnPolicy.LeadInsurerID = MstInsurerLead.InsurerID
LEFT OUTER JOIN dbo.TrnPolicy LastPolicy ON
dbo.TrnPolicy.ClientID = LastPolicy.ClientID
AND
dbo.TrnPolicy.FileNo = LastPolicy.FileNo
LEFT OUTER JOIN dbo.MstAgent ON
dbo.TrnPolicy.AgentID = dbo.MstAgent.AgentID
LEFT OUTER JOIN dbo.MstSubagent ON
dbo.TrnPolicy.SubagentID = dbo.MstSubagent.SubagentID
LEFT OUTER JOIN dbo.MstMarketerCodes ON
dbo.TrnPolicy.MarketerID = dbo.MstMarketerCodes.MarketerID
LEFT OUTER JOIN dbo.VieTeamSelect ON
dbo.TrnPolicy.TeamID = dbo.VieTeamSelect.TeamID
LEFT OUTER JOIN dbo.MstLink ON
dbo.TrnPolicy.LinkID = dbo.MstLink.LinkID
LEFT OUTER JOIN dbo.MstInsurer ON
dbo.MstInsurer.InsurerID= dbo.TrnClaim.InsurerID
INNER JOIN dbo.TrnLegend On
dbo.TrnLegend.ClientID = dbo.TrnClaim.ClientID
AND
dbo.TrnLegend.FileNo = dbo.TrnClaim.FileNo
WHERE
Upper(dbo.TrnLegend.LegendDescr) LIKE '%CANCELLED%'
Order By
dbo.TrnClaim.ClientID
, dbo.TrnClaim.FileNo
, dbo.TrnLegendRecordNo