且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从sql查询中获取重复记录

更新时间:2023-01-29 20:35:22

至少有几种可能性;)



查找和/或删除重复行 [ ^ ]

排名功能 [ ^ ]

使用DISTINCT消除重复项 [ ^ ]

如何从中删除重复的行SQL Server中的表 [ ^ ]

Hi all

Below is my query

SELECT ROW_NUMBER() OVER (ORDER BY om.OrderFormNo) AS SI ,
       om.OrderFormNo,
       om.OrderDate,
       ag.CompanyName AS Agency,
       sup.Name AS Supplier,
       dis.Name AS Distributor,
       it.ItemName,
       od.Quantity,
       od.Rate,
       odc.ReceivedQty,
       odc.ReceivedDate,
       odca.CancelQty,
       odca.CancelDate,
       od.Quantity-(odc.ReceivedQty+odca.CancelQty) AS Balance
FROM OrderMaster om
JOIN OrderDetails od ON om.OMID=od.OMIDFormRef
JOIN OrderDetailsChild odc ON od.ODID=odc.OrderDetailsIDRef
JOIN OrderDetailsCancel odca ON od.ODID=odca.OrderDetailsIDRef
JOIN ItemInfo it ON od.IIIDItemRef=it.IIID
JOIN Agency ag ON om.AIDAgencyRef=ag.AID
JOIN SupplierMainGroup sup ON om.SMGIDRef=sup.SMGID
JOIN MainDistributor dis ON om.MDIDRef=dis.MDID
WHERE sup.Name='trtrttrt'



and the output i am getting is


SI OrderFormNo OrderDate Agency Supplier Distributor ItemName Quantity Rate ReceivedQty ReceivedDate CancelQty CancelDate Balance


1 ORD2231REE 2013-11-21 Azad trtrttrt sds Shirtings 25 435 4 2013-11-21 3 2013-11-21 18

2 ORD2231REE 2013-11-21 Azad trtrttrt sds Shirtings 25 435 5 2013-11-21 3 2013-11-21 17

3 ORD2231REE 2013-11-21 Azad trtrttrt sds Shirtings 25 435 4 2013-11-21 3 2013-11-21 18

4 ORD2231REE 2013-11-21 Azad trtrttrt sds Shirtings 25 435 5 2013-11-21 3 2013-11-21 17


Now the problem is actually i am having only 2 rows i.e. SI. 1 and 2
but here it's duplicating and displaying same rows again.

Please tell me what i have to modify in the query.

Modify: I use Distinct and it's working fine only if i remove this line ROW_NUMBER() OVER (ORDER BY om.OrderFormNo) AS SI, else it's not working.
so can anyone tell me the alternate for ROW_NUMBER() OVER (ORDER BY om.OrderFormNo) AS SI line.

There is several possibilities at least ;)

Find and/or Delete Duplicate Rows[^]
Ranking functions[^]
Eliminating Duplicates with DISTINCT[^]
How to remove duplicate rows from a table in SQL Server[^]