Solved

How can I include missing records of two tables

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Managing Columnstore Indexes 2 18
Sql Join Problem 2 33
Microsoft SQL Server 2016 Setup..error 7 32
Sql Query 4 16
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

867 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

17 Experts available now in Live!

Get 1:1 Help Now