Avatar of PresidentLincoln
PresidentLincolnFlag for United States of America

asked on 

SQL: Need to run query on results of query

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?
DB2SQL

Avatar of undefined
Last Comment
PresidentLincoln
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

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".
Avatar of PresidentLincoln

ASKER

@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.
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of PresidentLincoln

ASKER

@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.
We would need a test case to set up the tables with data.  Then see your expected results.

Otherwise we are just guessing.
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
Avatar of PresidentLincoln

ASKER

@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.
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo