I have this file that has, among other things, a key, an account, and an address.
The account can occur more than once and when it does, its address is the same.
I want all of the addresses that occur only once and their associated accounts,
AND if the only reason an address is showing up multiple time is because each
record has the same associated account number, I want that account address
pair as well.
e.g.
key account address
01 11 11 Main
02 12 12 Main
03 11 11 Main
04 13 12 Main
05 14 13 Main
06 15 14 Main
07 16 15 Main
08 17 16 Main
09 16 15 Main
10 18 17 Main
I want the account, address pair 14, 13 Main because 13 Main only occurs once.
The same for 17, 16 Main and 18, 17 Main.
I want the account, address pair 11, 11 Main because - even though 11 Main occurs
twice - each occurrence is with the same account.
I don't want 12 Main because it occurs twice but with different accounts.
I don't want 15 Main because it occurs three times, twice with the same account but
once with a different one. If all three occurrences had been with the same account,
I would have wanted it.
The records with unique addresses and their associated accounts I can get by:
SELECT max(account), address FROM myfile GROUP BY
address HAVING count(address) = 1
But I am at a loss as how to add the records where the address count is
greater than 1 but only because every occurrence is associated with the
same account.
Is there some sort of recipe book for SQL? Syntax manuals are plentiful,
but is there one that tells you the SQL way to approach real-world problems?
I can sort the file by address and account extract the records in a single pass
with RPG, but that is so 1992 ;-)
Thanks,
Lance
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact
[javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.