Solved

How to include empty rows in a result set?

Posted on 2014-11-13
4
120 Views
Last Modified: 2014-11-13
Dear EE:

I have the following SQL SELECT:

SELECT
      A.CUSTOMERSTYLE_ID, B.STYLE_NAME, A.COLOR_ID, C.COLOR_NAME, A.SIZES,
      A.PRODORDER_NO, A.FULFILLMENTORDER, SUM( D.QUANTITY ), SUM( D.QUANTITYALLOW )
FROM CUSTOMER_POSTYSCOLS A, CUSTOMER_STYLES B, CUSTOMER_COLORS C, CUSTOMER_POSIZES D
WHERE
      A.COMPANY_ID = B.COMPANY_ID AND
      A.CUSTOMER_ID = B.CUSTOMER_ID AND
      A.CUSTOMERSTYLE_ID = B.CUSTOMERSTYLE_ID AND

      A.COMPANY_ID = C.COMPANY_ID AND
      A.CUSTOMER_ID = C.CUSTOMER_ID AND
      A.COLOR_ID = C.COLOR_ID AND

      A.COMPANY_ID = D.COMPANY_ID AND
      A.CUSTOMER_ID = D.CUSTOMER_ID AND
      A.CUSTOMERSTYLE_ID = D.CUSTOMERSTYLE_ID AND
      A.COLOR_ID = D.COLOR_ID AND

      A.COMPANY_ID = 0 AND
      A.CUSTOMER_ID = 7 AND
      A.CUSTOMERPO_ID = 1
GROUP BY A.CUSTOMERSTYLE_ID, B.STYLE_NAME, A.COLOR_ID, C.COLOR_NAME, A.SIZES, A.PRODORDER_NO, A.FULFILLMENTORDER, A.ROWNO
ORDER BY A.ROWNO

This SQL SELECT returns 2 rows, instead of 3.  This is because CUSTOMER_STYLES has 3 lines, but CUSTOMER_POSIZES references 2 --in other words, there is a record in CUSTOMER_STYLES that doesn't have child records in CUSTOMER_POSIZES.  I need to write this SQL SELECT, in a way, that it returns me 3 rows.

I will wait for your feedback.

Best regards, and thank you in advance.

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
  • 2
  • 2
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40440930
Give this a whirl.  Note the use of LEFT JOIN between A and D, which will return all the rows in A (i.e. 3 rows), along with values from D if there is a match (2) or NULLs if not (1).
SELECT
   A.CUSTOMERSTYLE_ID, B.STYLE_NAME, A.COLOR_ID, C.COLOR_NAME, A.SIZES, 
   A.PRODORDER_NO, A.FULFILLMENTORDER, SUM( D.QUANTITY ), SUM( D.QUANTITYALLOW )
FROM CUSTOMER_POSTYSCOLS A
   JOIN CUSTOMER_STYLES B ON A.COMPANY_ID = B.COMPANY_ID AND A.CUSTOMER_ID = B.CUSTOMER_ID AND A.CUSTOMERSTYLE_ID = B.CUSTOMERSTYLE_ID
   JOIN CUSTOMER_COLORS C ON A.COMPANY_ID = C.COMPANY_ID AND A.CUSTOMER_ID = C.CUSTOMER_ID AND A.COLOR_ID = C.COLOR_ID 
   LEFT JOIN CUSTOMER_POSIZES D ON A.COMPANY_ID = D.COMPANY_ID AND A.CUSTOMER_ID = D.CUSTOMER_ID AND A.CUSTOMERSTYLE_ID = D.CUSTOMERSTYLE_ID AND A.COLOR_ID = D.COLOR_ID 
WHERE
   A.COMPANY_ID = 0 AND 
   A.CUSTOMER_ID = 7 AND 
   A.CUSTOMERPO_ID = 1
GROUP BY A.CUSTOMERSTYLE_ID, B.STYLE_NAME, A.COLOR_ID, C.COLOR_NAME, A.SIZES, A.PRODORDER_NO, A.FULFILLMENTORDER, A.ROWNO
ORDER BY A.ROWNO

Open in new window

0
 

Author Comment

by:Conrado ZAVALA
ID: 40440958
Great! You are the best :) Thank you Jim.
0
 

Author Closing Comment

by:Conrado ZAVALA
ID: 40440962
I highly appreciate your help :)
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40440974
Thanks for the grade and compliments, good luck with your project.  -Jim
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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 ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
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…

632 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