• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 606
  • Last Modified:

How do I avoid duplicate entries when using multiple tables

I'm having trouble with multiple tables. How do I get  certain lines from duplicating. I attached a spreadsheet with items highlighted in yellow of what I would like to see. The un-highlighted areas are duplicate infotmation. I have trouble understanding what order tables should be selected and what joins to use. Thanks for any help you may give.
select RMA.DC_ID, RMA.RMA_BR_ID||''|| RMA.RMA_ID,  RMA.RMA_LINE_NBR, RMA.PART_ID, RMA.CUST_PART_ID, RMA.PART_DESCRIPTION, RMA.RMA_ORIG_QTY, RMA.RMA_OPEN_QTY, RMA.RMA_RECEIVED_QTY, RMA.RMA_REASON_CODE, RMA.RMA_LINE_TYPE, RMA.VENDOR_IND, RMA.RTC_REASON_CODE, RMA1.RMA_CREATE_DATE, RMA1.RMA_STATUS, RMA1.CUST_ID, RMA1.CUST_NAME, RMA1.END_USER_RMA_ID, RMA2.DESCRIPTION_NBR, RMA2.DESCRIPTION
from CSI.RMA_LINE RMA, CSI.RMA_HEADER RMA1, CSI.RMA_DESCRIPTION RMA2
where RMA.RMA_BR_ID = RMA1.RMA_BR_ID
	and RMA.RMA_ID = RMA1.RMA_ID
	and RMA.DC_ID = RMA1.DC_ID
	and RMA1.DC_ID = RMA2.DC_ID
	and RMA1.RMA_BR_ID = RMA2.RMA_BR_ID
	and RMA1.RMA_ID = RMA2.RMA_ID
	and RMA.RMA_LINE_NBR = RMA2.RMA_LINE_NBR
	and (RMA1.CUST_ID in ('60118296', '60216776', '60305816', '60588621', '60691030'))
                and RMA1.RMA_CREATE_DATE >= @VARIABLE ('BEGINNING DATE')
                and RMA1.RMA_CREATE_DATE <= @VARIABLE ('ENDING DATE')

Open in new window

RMA-Report.xls
0
Jbancr1
Asked:
Jbancr1
1 Solution
 
SharathData EngineerCommented:
You can use GRIUP BY. You have many columns in your SELECT statement but very few in the attached xls. Can you bit elobarate on sample data in your three tables and desired output?
0
 
germano_ptCommented:
First, avoid cartesian joins (represented by the comma)... They cross all records from table A with all records from table B... which will them be crossed with all records from table C, and so on...

You them filter-out the invalid "crossed records" in the WHERE clause, but it is very inefficient and prone to errors.

You should join with the JOIN keyword, indicating the join condition with the ON keyword (in fact, you'll be performing an INNER JOIN... read more in http://en.wikipedia.org/wiki/Join_(SQL) ).

Now, for the column data you don't want, notice that all the rows are valid distinct records. If you want to hide the duplicate column's values, you should do it somewhere else with code, not in the SQL statement.
SELECT
  RMA.DC_ID, RMA.RMA_BR_ID||''|| RMA.RMA_ID,  RMA.RMA_LINE_NBR, RMA.PART_ID, RMA.CUST_PART_ID, RMA.PART_DESCRIPTION, RMA.RMA_ORIG_QTY, RMA.RMA_OPEN_QTY, RMA.RMA_RECEIVED_QTY, RMA.RMA_REASON_CODE, RMA.RMA_LINE_TYPE, RMA.VENDOR_IND, RMA.RTC_REASON_CODE, RMA1.RMA_CREATE_DATE, RMA1.RMA_STATUS, RMA1.CUST_ID, RMA1.CUST_NAME, RMA1.END_USER_RMA_ID, RMA2.DESCRIPTION_NBR, RMA2.DESCRIPTION
 
FROM CSI.RMA_LINE RMA
 
 JOIN CSI.RMA_HEADER RMA1
  ON RMA.RMA_ID = RMA1.RMA_ID
   AND RMA.RMA_BR_ID = RMA1.RMA_BR_ID
   AND RMA.DC_ID = RMA1.DC_ID
 
 JOIN CSI.RMA_DESCRIPTION RMA2
  ON RMA1.DC_ID = RMA2.DC_ID
   AND RMA1.RMA_BR_ID = RMA2.RMA_BR_ID
   AND RMA1.RMA_ID = RMA2.RMA_ID
   AND RMA.RMA_LINE_NBR = RMA2.RMA_LINE_NBR
 
WHERE (RMA1.CUST_ID IN ('60118296', '60216776', '60305816', '60588621', '60691030'))
  AND RMA1.RMA_CREATE_DATE >= @VARIABLE ('BEGINNING DATE')
  AND RMA1.RMA_CREATE_DATE <= @VARIABLE ('ENDING DATE')

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now