Link to home
Start Free TrialLog in
Avatar of Conrado ZAVALA
Conrado ZAVALAFlag for Honduras

asked on

How can I include missing records of two tables

Dear Experts-Exchange:

I have the following two tables:

Table #1: CUSTOMER_POSIZES
Columns: COMPANY_ID,CUSTOMER_ID,CUSTOMERSTYLE_ID,CUSTOMERPO_ID,CUSTOMERSIZE_ID
Row #1: 0,8,'GARMENT #3',2,'6'
Row #2: 0,8,'GARMENT #3',2,'XS'
Row #3: 0,8,'GARMENT #3',2,'S'
Row #4: 0,8,'GARMENT #3',2,'M'

and

Table #2: CUSTOMER_SIZESPSTYLE
Columns: COMPANY_ID,CUSTOMER_ID,CUSTOMERSTYLE_ID,CUSTOMERSIZE_ID
Row #1: 0,8,'GARMENT #3','XS'
Row #2: 0,8,'GARMENT #3','S'
Row #3: 0,8,'GARMENT #3','M'
Row #4: 0,8,'GARMENT #3','L'

Notice that CUSTOMERSIZE_ID = '6' is not present in CUSTOMER_SIZESPSTYLE list.

I need to write an SQL SELECT that can give me the following result set:

Row #1: '6'
Row #2: 'XS'
Row #3: 'S'
Row #4: 'M'
Row #5: 'L'

I will appreciate your help.

Best regards,

Conrado
Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Please define WHY you expect that output? You dont explain why you want the output specified. It displays 5 rows, what are the 5 rows?

You need to explain the logic behind WHY you expect to get that output. We could guess but then you may end up with a statement that works on THAT set of data but not on others.
ASKER CERTIFIED SOLUTION
Avatar of Simon
Simon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ludo Van den Bosch
Ludo Van den Bosch

I don't agree with you. Your result would give:

Row #1: '6'
Row #2: 'XS'
Row #3: 'S'
Row #4: ,'M'
Row #5: 'XS'
Row #6: 'S'
Row #7: 'M'
Row #8: 'L'

because you first do a distinct on the seperate tables and then you union them together.

I think the following query is the correct one:

select distinct CUSTOMERSIZE_ID from (
select CUSTOMERSIZE_ID from CUSTOMER_POSIZES
union
select CUSTOMERSIZE_ID from CUSTOMER_SIZESPSTYLE
)

I first union them and then distinct the selection
@Ludo - UNION removes duplicates, UNION ALL does not. That's why I used the former.
UNION (Transact-SQL)
Avatar of Conrado ZAVALA

ASKER

Thank you SimonAdept for providing a solution to my case.  With your recommendation, I ended up building the following SQL SELECT:

SELECT B.SORT_ORDER, A.CUSTOMERSIZE_ID, C.QUANTITY, C.QUANTITYALLOW FROM CUSTOMER_SIZESPSTYLE A
INNER JOIN CUSTOMER_SIZES B ON A.COMPANY_ID = B.COMPANY_ID AND A.CUSTOMER_ID = B.CUSTOMER_ID AND A.CUSTOMERSIZE_ID = B.CUSTOMERSIZE_ID
LEFT JOIN CUSTOMER_POSIZES C ON A.COMPANY_ID = C.COMPANY_ID AND A.CUSTOMER_ID = C.CUSTOMER_ID AND A.CUSTOMERSTYLE_ID = C.CUSTOMERSTYLE_ID AND A.CUSTOMERSIZE_ID = C.CUSTOMERSIZE_ID
WHERE
      A.COMPANY_ID = 0 AND
      A.CUSTOMER_ID = 8 AND
      A.CUSTOMERSTYLE_ID = 'GARMENT #3'
UNION
SELECT B.SORT_ORDER, A.CUSTOMERSIZE_ID, A.QUANTITY, A.QUANTITYALLOW FROM CUSTOMER_POSIZES A
INNER JOIN CUSTOMER_SIZES B ON A.COMPANY_ID = B.COMPANY_ID AND A.CUSTOMER_ID = B.CUSTOMER_ID AND A.CUSTOMERSIZE_ID = B.CUSTOMERSIZE_ID
WHERE
      A.COMPANY_ID = 0 AND
      A.CUSTOMER_ID = 8 AND
      A.CUSTOMERPO_ID = 2 AND
      A.CUSTOMERSTYLE_ID = 'GARMENT #3' AND
      A.COLOR_ID = 'CARD. RED'
ORDER BY SORT_ORDER

Now, I am getting the output I need in my program :)

I also want to thank Neilsr and Ludo Van den Bosch for getting involved in this case.  I highly appreciate everyone's help.

Thank you all,

Conrado