Solved

How to include empty rows in a result set?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

776 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