Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all

Below is my query

SQL
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.
Posted
Updated 20-Nov-13 21:56pm
v6
Comments
Sergey Vaselenko 21-Nov-13 4:23am    
These joins select rows what you have:

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

It seems you should group rows before using ROW_NUMBER().
Also add fields to the ORDER BY clause to get the same order every time.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900