SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple Joins - Duplicate Rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

huddy1985
Starting Member

United Kingdom
6 Posts

Posted - 07/10/2014 :  21:54:55  Show Profile  Reply with Quote
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

India
95 Posts

Posted - 07/11/2014 :  02:29:25  Show Profile  Reply with Quote
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

United Kingdom
6 Posts

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

huddy1985
Starting Member

United Kingdom
6 Posts

Posted - 07/11/2014 :  21:58:09  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 07/15/2014 :  12:30:54  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000