Solved

How to include empty rows in a result set?

Posted on 2014-11-13
4
118 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 65

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 65

Expert Comment

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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

739 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