我试图在同一个表中的3列中找到重复数据。我正在寻找具有相同ReferenceNumber,PremiseId& amp; DatePlaced。
例如,出现了具有DatePlaced 02/08/17的PremiseId 67890的ReferenceNumber为12345的订单,因为满足所有三个条件。
这是我到目前为止所做的,这是从本网站的另一个答案中获取的。
SELECT ReferenceNumber, PremiseId, DatePlaced
FROM [ypolive_Integration].[dbo].[OrderHeaders]
WHERE ReferenceNumber in
(SELECT ReferenceNumber FROM [ypolive_Integration].[dbo].[OrderHeaders]
GROUP BY ReferenceNumber
HAVING COUNT(*)>1)
Order By DatePlaced desc
这是ReferenceNumber,PremiseId&的返回结果。 DatePlaced。然而,这显示了所有数据,我只希望它显示符合所有三个标准的重复项。
我非常喜欢这种事情,如果这是完全错误的话,请道歉!
现在用以下代码解决这个问题:
select ReferenceNumber, PremiseId , CONVERT(VARCHAR(10),DatePlaced, 112) as
DatePlaced,count(1) as cnt
from [ypolive_Integration].[dbo].[OrderHeaders]
group by ReferenceNumber, PremiseId , CONVERT(VARCHAR(10),DatePlaced, 112)
having count(1) > 1
order by DatePlaced desc
答案 0 :(得分:1)
您可以查询如下:
Select * from (
SELECT --ReferenceNumber, PremiseId, DatePlaced,
*
,RowN = Row_Number() over (partition by ReferenceNumber, PremiseId order by DatePlaced desc)
FROM [ypolive_Integration].[dbo].[OrderHeaders]
) a
Where a.RowN > 1
答案 1 :(得分:1)
请尝试以下查询
select ReferenceNumber, PremiseId , CONVERT(VARCHAR(10),DatePlaced, 112) as
DatePlaced,count(1) as cnt
from [ypolive_Integration].[dbo].[OrderHeaders]
group by ReferenceNumber, PremiseId , CONVERT(VARCHAR(10),DatePlaced, 112)
having count(1) > 1
答案 2 :(得分:0)
SELECT ReferenceNumber
,PremiseId
,DatePlaced
,convert(VARCHAR(100), ReferenceNumber) + ' - ' + convert(VARCHAR(100), PremiseId) + ' - ' + convert(VARCHAR(100), DatePlaced, 121) combined --Remove this line if not needed
FROM [ypolive_Integration].[dbo].[OrderHeaders]
WHERE convert(VARCHAR(100), ReferenceNumber) + ' - ' + convert(VARCHAR(100), PremiseId) + ' - ' + convert(VARCHAR(100), DatePlaced, 121) IN (
SELECT convert(VARCHAR(100), ReferenceNumber) + ' - ' + convert(VARCHAR(100), PremiseId) + ' - ' + convert(VARCHAR(100), DatePlaced, 121) combined
GROUP BY convert(VARCHAR(100), ReferenceNumber) + ' - ' + convert(VARCHAR(100), PremiseId) + ' - ' + convert(VARCHAR(100), DatePlaced, 121)
HAVING COUNT(*) > 1
)
ORDER BY DatePlaced DESC
答案 3 :(得分:-1)
尝试使用此查询:
SELECT ReferenceNumber, PremiseId, DatePlaced
FROM [ypolive_Integration].[dbo].[OrderHeaders]
WHERE ReferenceNumber in
(SELECT ReferenceNumber FROM [ypolive_Integration].[dbo].[OrderHeaders]
GROUP BY ReferenceNumber HAVING COUNT(*)>1)
AND PremiseId in (SELECT PremiseId FROM [ypolive_Integration].[dbo].[OrderHeaders] GROUP BY PremiseId HAVING COUNT(*)>1)
AND DatePlaced in (SELECT DatePlaced FROM [ypolive_Integration].[dbo].[OrderHeaders] GROUP BY DatePlaced HAVING COUNT(*)>1)