Solved

How can I include missing records of two tables

Posted on 2014-11-29
5
98 Views
Last Modified: 2014-12-03
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
0
Comment
Question by:Conrado ZAVALA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40471802
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.
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40471808
This statement will give you the combined list of unique values

select DISTINCT CUSTOMERSIZE_ID from CUSTOMER_POSIZES
UNION
select DISTINCT CUSTOMERSIZE_ID from CUSTOMER_SIZESPSTYLE

Open in new window



The union statement will omit duplicates.

What the above won't do is give the sort order that you want. You'd need another table such as
create table SIZE_LIST (
CUSTOMERSIZE_ID, SORTORDER)
Insert into Size_LIST
Values ('6',10),
('XS',20),
('S',30),
('M',40)
( 'L',50)

You could then select the sort order value from that table

select DISTINCT SORTORDER,CUSTOMERSIZE_ID from CUSTOMER_POSIZES  C inner join SIZE_LIST S on C.CUSTOMERSIZE_ID =S.CUSTOMERSIZE_ID
UNION
select SORTORDER,DISTINCT CUSTOMERSIZE_ID from CUSTOMER_SIZESPSTYLE  C inner join SIZE_LIST S on C.CUSTOMERSIZE_ID =S.CUSTOMERSIZE_ID
Order By SortOrder

Open in new window

0
 

Expert Comment

by:Ludo Van den Bosch
ID: 40471865
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
0
 
LVL 18

Expert Comment

by:Simon
ID: 40471890
@Ludo - UNION removes duplicates, UNION ALL does not. That's why I used the former.
UNION (Transact-SQL)
0
 

Author Closing Comment

by:Conrado ZAVALA
ID: 40479232
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question