Solved

How can I include missing records of two tables

Posted on 2014-11-29
5
92 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:
SimonAdept 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:SimonAdept
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now