Avatar of Zolf
Zolf
Flag for United Arab Emirates asked on

MSSQL Query help

Hello there,

I have this query which is returning some redundant data. Can somebody please help me to correct this query. I have also attached the table relationship.

SELECT
    dbo.supplierOrder.sectionID,
    dbo.supplierOrder.supplierID,
    dbo.supplierOrder.supplierOrderNo,
    dbo.supplierOrder.supplierOrderCreated,
    dbo.supplierOrder.supplierOrderConfirmStatus,
    dbo.supplierOrderDetails.productID,
    dbo.supplierOrderDetails.orderQty,
    dbo.supplierOrderReceive.invoiceno,
    dbo.supplierOrderReceive.supplierInvoiceno,
    dbo.supplierOrderReceive.ID,
    dbo.supplierOrderReceiveDetail.qtyArrived
FROM
    dbo.supplierOrder
INNER JOIN
    dbo.supplierOrderDetails
ON
    (
        dbo.supplierOrderDetails.supplierOrderID = dbo.supplierOrder.ID)
INNER JOIN 
        dbo.supplierOrderReceive
ON  
     (        
       dbo.supplierOrderReceive.supplierOrderID = dbo.supplierOrder.ID)
INNER JOIN 
        dbo.supplierOrderReceiveDetail
ON  
     (        
       dbo.supplierOrderReceiveDetail.supplierOrderReceiveID = dbo.supplierOrderReceive.ID)       
WHERE
    dbo.supplierOrder.ID = 1 ;

Open in new window

table-relationship.png
table-relation.png
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Zolf

8/22/2022 - Mon
Zolf

ASKER
here is the output that I get

which is not exactly what I was want. the last column qtyArrived should show only its related qty but here as you can see it is repeating for each of the productID

sectionID      supplierID      supplierOrderNo      supplierOrderCreated      supplierOrderConfirmStatus      productID      orderQty      invoiceno      supplierInvoiceno      ID      qtyArrived
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      223444      1      2
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      223444      1      2
1      1      (null)      2013-12-10 12:35:03      true      6      4      (null)      223444      1      2
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      223444      1      8
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      223444      1      8
1      1      (null)      2013-12-10 12:35:03      true      6      4      (null)      223444      1      8
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      565333      2      1
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      565333      2      1
1      1      (null)      2013-12-10 12:35:03      true      6      4      (null)      565333      2      1
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      565333      2      4
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      565333      2      4
1      1      (null)      2013-12-10 12:35:03      true      6      4      (null)      565333      2      4
Phil Davidson

I would use the keyword DISTINCT before
dbo.supplierOrderReceiveDetail.qtyArrived

Open in new window

Zolf

ASKER
thanks for your comment.can you please give me the full query.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Phil Davidson

SELECT
    dbo.supplierOrder.sectionID,
    dbo.supplierOrder.supplierID,
    dbo.supplierOrder.supplierOrderNo,
    dbo.supplierOrder.supplierOrderCreated,
    dbo.supplierOrder.supplierOrderConfirmStatus,
    dbo.supplierOrderDetails.productID,
    dbo.supplierOrderDetails.orderQty,
    dbo.supplierOrderReceive.invoiceno,
    dbo.supplierOrderReceive.supplierInvoiceno,
    dbo.supplierOrderReceive.ID,
   DISTINCT dbo.supplierOrderReceiveDetail.qtyArrived
FROM
    dbo.supplierOrder
INNER JOIN
    dbo.supplierOrderDetails
ON
    (
        dbo.supplierOrderDetails.supplierOrderID = dbo.supplierOrder.ID)
INNER JOIN 
        dbo.supplierOrderReceive
ON  
     (        
       dbo.supplierOrderReceive.supplierOrderID = dbo.supplierOrder.ID)
INNER JOIN 
        dbo.supplierOrderReceiveDetail
ON  
     (        
       dbo.supplierOrderReceiveDetail.supplierOrderReceiveID = dbo.supplierOrderReceive.ID)       
WHERE
    dbo.supplierOrder.ID = 1 ;

Open in new window

Zolf

ASKER
no this give me error

 Incorrect syntax near the keyword 'DISTINCT'.

I tried to add DISTINCT at the start and it ran the query but does not give me the desired result
Pratima

I just verified the data you have provided , I am not able to get complete record duplication here
 ID and       qtyArrived
is differenct for all cloumns
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Zolf

ASKER
what do you mean. i have printed my result,it is duplicating the data. for e.g. for all the productID it is repeating the same qtyArrived. my final result should return only 4 records.
Pratima

sectionID      supplierID      supplierOrderNo      supplierOrderCreated      supplierOrderConfirmStatus      productID      orderQty      invoiceno      supplierInvoiceno      ID      qtyArrived
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      223444      1      2
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      223444      1      2
1      1      (null)      2013-12-10 12:35:03      true      6      4      (null)      223444      1      2

you mean to say you need one record for this ? what will be that
Zolf

ASKER
i am expecting this

sectionID      supplierID      supplierOrderNo      supplierOrderCreated      supplierOrderConfirmStatus      productID      orderQty      invoiceno      supplierInvoiceno      ID      qtyArrived
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      223444      1      8
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      223444      1      2
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      565333      2      1
1      1      (null)      2013-12-10 12:35:03      true      6      4      (null)      565333      2      4
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Anthony Perkins

Can you explain how you would derive those rows from the data you posted earlier?  I am not seeing any rhyme or reason.  Take that first row:
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      223444      1      8

I assume it comes from the following rows:
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      223444      1      2
1      1      (null)      2013-12-10 12:35:03      true      5      8      (null)      223444      1      8

Is it the"last" one that you are looking for?  If so, can you define the "last" one?
Anthony Perkins

For the second row:
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      223444      1      2

I am assuming it is coming from:
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      223444      1      2
1      1      (null)      2013-12-10 12:35:03      true      1      3      (null)      223444      1      8

But here you appear to have picked the "first" one.  

Perhaps it does not matter which one is returned.
Zolf

ASKER
thanks for your comments.
see the invoice numbers in the result.  the idea is to get the products which the supplier has sent to the client. the products could have come in one batch or in small batches. the productid 1 has come in 2 batches(the orderQty by the client was 3 but it qtyArrived in 2 batches of 1 and 2). see the attached file.
table-relationship.png
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Sharath S

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Zolf

ASKER
thanks