我在SQL Server 2005中运行的查询中遇到数据重复问题:
`SELECT i.datecreated, i.CompanyName, i.City, i.State, i.Phone, i.InsCoID, i.Address1, i.Zip,
case i.Reviewed when 0 then 'Pending Approval' else 'Active' end,
r.FirstName + ' ' + r.LastName AS Adjuster, r.Phone
FROM V_InsRepresentative r
RIGHT JOIN V_InsCompany i ON r.InsCoID = i.InsCoID
WHERE i.DateCreated >= '5/18/2014'
And i.DateCreated < '5/26/2014'
AND i.Status = '1'
ORDER BY i.state, i.companyname`
其中一条记录重复,因为有两个调整器。有没有办法只有一个调整器显示?
答案 0 :(得分:0)
尝试使用调整器上的Max()对数据进行分组,以便只显示一个调整器:
SELECT i.datecreated ,
i.CompanyName ,
i.City ,
i.State ,
i.Phone ,
i.InsCoID ,
i.Address1 ,
i.Zip ,
CASE i.Reviewed
WHEN 0 THEN 'Pending Approval'
ELSE 'Active'
END ,
Max(r.FirstName + ' ' + r.LastName) AS Adjuster ,
r.Phone
FROM V_InsRepresentative r
RIGHT JOIN V_InsCompany i ON r.InsCoID = i.InsCoID
WHERE i.DateCreated >= '5/18/2014'
AND i.DateCreated < '5/26/2014'
AND i.Status = '1'
group by
i.datecreated ,
i.CompanyName ,
i.City ,
i.State ,
i.Phone ,
i.InsCoID ,
i.Address1 ,
i.Zip ,
CASE i.Reviewed
WHEN 0 THEN 'Pending Approval'
ELSE 'Active'
END ,
r.Phone
ORDER BY i.state ,
i.companyname
答案 1 :(得分:0)
我建议在from
条款中修复此问题:
FROM (SELECT r.*, row_number() over (partition by InsCoID order by INsCoId) as seqnum
FROM V_InsRepresentative r
) r
RIGHT JOIN V_InsCompany i ON r.InsCoID = i.InsCoID and r.seqnum = 1
这将任意选择r
表中的一行,我相信这是重复检查员的意思。