我已编辑SQL以使用DISTINCT但是我仍然获得与使用GROUP BY时相同的数据。有什么建议? 当我使用GROUP BY时,它要求包含select子句中的所有字段,有人能告诉我为什么会这样吗?
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
答案 0 :(得分:0)
使用Group By
时,请指定属性列表。包含GROUP BY
子句的查询会根据属于分组集的每个属性值的唯一组合,为每个组生成一行。
因此,在查询中指定GROUP BY
时,所有后续步骤(HAVING,SELECT等)都只能指定每个组具有标量(奇异)值的表达式。这些表达式可以包括GROUP BY列表中的列或表达式,如TrnClaim.ClientID - 或聚合函数,例如COUNT(TrnClaim.ClientID)。
正如我所看到的,您在上面的查询中没有包含任何聚合函数,这就是为什么当你写下Group By(TrnClaim.ClientID)或任何其他列时,没有创建任何组或改写每一行成为一个唯一的组,所以它要求包含select子句中的所有字段。
这就是为什么您使用Distinct和GROUP BY获取相同数据的原因,因为没有创建任何组。