Solved

How to include empty rows in a result set?

Posted on 2014-11-13
4
108 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
Comment Utility
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
Comment Utility
Great! You are the best :) Thank you Jim.
0
 

Author Closing Comment

by:Conrado ZAVALA
Comment Utility
I highly appreciate your help :)
0
 
LVL 65

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

771 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

9 Experts available now in Live!

Get 1:1 Help Now