Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

multiple table join gone crazy

$
0
0

I seem to be getting more data than I need...  Basically joining 3 table on a ReceiverID.  The first table has one row possible, then the second table could have multiple(many) and the third the same(many).  If I leave the second join out it doesn't run an additional row, but as soon as I add in the second join it adds a row for each of the rows on the second join. The Rcvr_ID is the first returned data for each select. How do I get rid of the duplicates for the second join. It should be a basic join and only have two rows total for the whole select rather than 4 it is returning.  I trimmed some of the data out, but you can get the point.

SELECT *
FROM rcvr r
INNER JOIN rcvr_rcpts rcpts ON r.Rcvr_ID = rcpts.Rcvr_ID
JOIN rcvr_invnbrs rcvri ON rcpts.Rcvr_ID = rcvri.Rcvr_ID
WHERE r.recpt_date >= @StartDate AND r.recpt_date <= @EndDate AND (@RcvngRptNo IS NULL OR r.Rcvng_Rpt_No = @RcvngRptNo) AND (@PoNbr IS NULL OR r.PONbr = @PoNbr) AND (@VendorID IS NULL OR r.venNbr = @VendorID)
ORDER BY r.rcvng_rpt_no

Returned data:

5838192012-11-15 00:00:002012-11-15 00:00:00121890NULL58317490825110899C
5838192012-11-15 00:00:002012-11-15 00:00:00121890NULL58317490825110899C
5838192012-11-15 00:00:002012-11-15 00:00:00121890NULL58317490825110899C
5838192012-11-15 00:00:002012-11-15 00:00:00121890NULL58317490825110899C


Data:

select * from rcvr where Rcvr_ID = '583819'

5838192012-11-15 00:00:002012-11-15 00:00:00121890NULL58317490825110899C

select * from rcvr_rcpts where Rcvr_ID = '583819'

5838191 5190.80 038.1600 01-640000-900C5.00EA0.0038.1600190.80190.80
5838192 544.20 08.8400 01-640000-900C5.00EA0.008.840044.2044.20

select * from rcvr_invnbrs where Rcvr_ID = '583819'

5838191 1108991.0000
5838192 1108992.0000

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>