We help IT Professionals succeed at work.

SQL: Need to run query on results of query

Steven Ruiz
Steven Ruiz asked
on
Medium Priority
63 Views
Last Modified: 2020-03-10
I need to run a query on the results of another query.

First I have a query that gives me a list of customers and the products they own from my company, if those products were opened in 2019.  I need to take that list of customers and find out, of those, which ones had late fees and how much the late fees were.  Here's an example of the query I wrote that gives me the list of customers:

SELECT DISTINCT AC.CUST_NBR

FROM DB2INST1.ACCOUNT.AC

INNER JOIN DB2INST1.PRODUCTS PR ON AC.CUST_NBR = PR.CUST_NBR AND AC.ACCOUNT_NBR = PR.ACCOUNT_NBR

WHERE PR.TYPE IN (26,27) AND
AC.OPEN_DATE <= '2019-01-01' AND
(AC.CLOSE_DATE >= '2019-01-01' OR
AC.CLOSE_DATE IS NULL)

Open in new window


I need to now take the list of customers that query products and join in another FEES table to see which of those customers if any had fees and what the fee amount was.  What would be the easiest way to do this?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019

Commented:
If you want all customers even if they have no fees, seems like a "left join fees f on f.cust_nbr=ac.cust_nbr" would work.

If you only want customers with fees, remove the "left".
Steven RuizApplication Analyst

Author

Commented:
@slightwv, what about the WHERE clause where I will specify the types of fees.  So something like this:

SELECT DISTINCT AC.CUST_NBR

FROM DB2INST1.ACCOUNT.AC

INNER JOIN DB2INST1.PRODUCTS PR ON AC.CUST_NBR = PR.CUST_NBR AND AC.ACCOUNT_NBR = PR.ACCOUNT_NBR
LEFT OUTER JOIN DB2INST1.FEES FEE ON AC.CUST_NBR = FEE.CUST_NBR

WHERE PR.TYPE IN (26,27) AND
AC.OPEN_DATE <= '2019-01-01' AND
(AC.CLOSE_DATE >= '2019-01-01' OR
AC.CLOSE_DATE IS NULL) AND
FEE.TYPE = 'LATE'

Open in new window


It seems like it would only pull in customers that had a late fee if I added that bit of code.
CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019
Commented:
Try it and see.

It should be the same as:
LEFT OUTER JOIN DB2INST1.FEES FEE ON AC.CUST_NBR = FEE.CUST_NBR and FEE.TYPE = 'LATE'
Steven RuizApplication Analyst

Author

Commented:
@slightwv, every customer number row is coming back with a fee.  I was expecting to see a lot of NULLS in there, but every customer listed has a fee associated with it, and I know that's not the case.
CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019

Commented:
We would need a test case to set up the tables with data.  Then see your expected results.

Otherwise we are just guessing.
Tomas Helgi JohannssonDatabase Administrator / Software Engineer
CERTIFIED EXPERT

Commented:
Hi,

I suggest you break the problem into two separate queries and use CTE syntax to join those queries together.
Something like
WITH customerlist as (
<your customerlist query>
), customerfees as (
<your customerfees query which could be a group by query to sum upp all the fees>
)
select .... from customerlist cl,  customerfees cf
where cl.cust_nbr = cf.cust_nbr ...

Open in new window


This is a good way to divide and conquer a problem and then merge the results into one. :)

Regards,
    Tomas Helgi
Steven RuizApplication Analyst

Author

Commented:
@slightwv, you were correct.  I had a small error in my code but I was able to fix it and get the results I need.  Thanks.