Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to include empty rows in a result set?

Posted on 2014-11-13
4
Medium Priority
?
126 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
  • 2
  • 2
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

971 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