We help IT Professionals succeed at work.
Get Started

SQL: Need to run query on results of query

PresidentLincoln
on
134 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 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE