query help...rookie here!
610952Nov 28 2007 — edited Dec 2 2007Hello,
I need some help. I have a problem I am not too sure on how to approach. I have two tables that look some thing like this:
Table 1
CRITERIA_ID DESCRIPTION
1 Product A
2 Product B
3 Product C
4 Product D
5 Product E
6 Product F
7 Product G
8 Product H
9 Product I
10 Product J
Table 2
ACCOUNT_NO CRITERIA_ID
000000001 1
000000002 1
000000003 1
000000004 1
000000005 1
000000001 2
000000002 2
000000003 2
000000004 2
000000001 3
000000002 3
000000002 4
000000001 5
000000002 5
000000001 6
000000002 7
000000003 7
000000002 8
000000004 8
000000004 9
000000004 10
000000006 10
Table 1 - the CRITERIA_ID is unique
Table 2 - ACCOUNT_NO can occurr for more than one CRITERIA_ID and CRITERIA_ID can occurr for more than one ACCOUNT_NO
What I want to be able to do is query for the account_no, criteria_id and description
for the account_no which occurs for the most criteria_id, then the next account_no which occurs with the next most criteria_id and so on
until all 10 criteria_id are found. I want to be able to find all the criteria_id's using the least amount of account_no's. Is this even possible?
The result I am looking for would be some thing like this:
CRITERIA_ID DESCRIPTION ACCOUNT_NO
1 Product A 000000001
2 Product B 000000001
3 Product C 000000001
5 Product E 000000001
6 Product F 000000001
4 Product D 000000002
7 Product G 000000002
8 Product H 000000002
9 Product I 000000004
10 Product J 000000004
The only thing I have been able to do is join the tables:
select A.CRITERIA_ID,
A.DESCRIPTION,
B.ACCOUNT_NO
from TABLE_1 A,TABLE_2 B
where A.CRITERIA_ID=B.CRITERIA_ID
order by A.CRITERIA_ID
Any help/direction would be appreciated...
Thanks,
Gman