Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.
Hi, hoping for some quick guidance on what should be a very easy SQL statement...
I would like to find all accounts in a table where there is more than one record in the table for that account where the end date is 0. Each account should only have one record in the table without an end date, so I'm trying to find those accounts that have more than one record with no end date.
Something like:
select account
from myfile
where enddate = 0
group by account
having count(*) > 1
order by account
But this will also find accounts with more than one record as long as one of them has an enddate = 0, and I only want to see those where multiple records for that account have enddate = 0.
Any help would be appreciated... thanks!
select account from myfile group by account, enddate having count(*) > 1 and enddate = 0 order by account
Yes, that was it... thanks Bob!
WHERE condidtions are applied before GROUPing. The query as you originally presented it should work as is. Is there more that you're not showing us? Some JOINs maybe?
Dale, no this is the whole statement. Mine also showed accounts with just one open record (no end date) as long as there were multiple records for the account, but Bob's only shows accounts with multiple OPEN records. Bob's produces the result I needed.