Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple Joins - Duplicate Rows

Author  Topic 

huddy1985
Starting Member

6 Posts

Posted - 2014-07-10 : 21:54:55
please could someone help me with the below query. have tried joining several tables and the result displays duplicate rows of virtually every line/row. i have tried using distinct but failed miserably. thanks



select purchaseorders.traderid,
suppliers.name
stockbatches.partid,
allpartmaster.partdesc,
allpartmaster.prodgroup,
goodsreceiptitems.createddate,
stockbatches.warehouseid,
stockbatches.locationid,
stockbatches.quantity,
stockbatches.status,
stockbatches.batchnumber,
stockbatches.batchid,
stockbatches.origin,
stockbatches.unitvalue,
goodsreceiptitems.orderid from stockbatches
inner join allpartmaster on stockbatches.partid = allpartmaster.partnum
inner join goodsreceiptbatches on stockbatches.batchnumber = goodsreceiptbatches.stockbatchid
inner join goodsreceiptitems on goodsreceiptbatches.goodsreceipt = goodsreceiptitems.shipmentid
inner join purchaseorders on goodsreceiptitems.orderid = purchaseorders.id
inner join suppliers on purchaseorders.traderid = suppliers.id
where allpartmaster.prodgroup = 'RMTAILS' and stockbatches.quantity >0

mmkrishna1919
Yak Posting Veteran

95 Posts

Posted - 2014-07-11 : 02:29:25
The duplicate records because of one to many relationship between the tables which you have joined. Look at the columns like stockbatches.status by adding additional conditions (status= 'active')on these kind of columns you can avoid duplicate records.
Anyway by adding distinct in select, you get only unique records in result set.

M.MURALI kRISHNA
Go to Top of Page

huddy1985
Starting Member

6 Posts

Posted - 2014-07-11 : 21:41:03
If I use alias will this help at all? Thanks
Go to Top of Page

huddy1985
Starting Member

6 Posts

Posted - 2014-07-11 : 21:58:09
If I use alias will this help at all? Thanks


quote:
Originally posted by mmkrishna1919

The duplicate records because of one to many relationship between the tables which you have joined. Look at the columns like stockbatches.status by adding additional conditions (status= 'active')on these kind of columns you can avoid duplicate records.
Anyway by adding distinct in select, you get only unique records in result set.

M.MURALI kRISHNA

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-07-15 : 12:30:54
quote:
Originally posted by huddy1985

If I use alias will this help at all? Thanks

Nope.

In order for us to help, you'll have to show us your tables and provide sample data. Since we don't have access to your database we are just going to play 20+ questions. So, the best thing is for you follow the link below that describes how to post your question. That includes SQL Statements for creating the table(s) and inserting data into those tables. If you can do that and show us what you want for output, we can help guide you into a solution.

http://www.sqlservercentral.com/articles/Best+Practices/61537/

Go to Top of Page
   

- Advertisement -