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
Solved

How can I include missing records of two tables

Posted on 2014-11-29
5
96 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

840 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