• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

How to know if one field of the database table has many associated records

I have a table T which has rows A,B,C,D

Records shown under A have records associated against it under B

How do I determine if a record under A has more than one unique value associated with it under B..

Remember the Table has got more than 70K+ Records

Would like to see the output some thing like

A          B              Count
---       -----           ----------
1           12              4
2            0               1
3            23              3

I hope the question is understood and also the expected output

Please help experts need a solution at the earliest.......
  • 4
  • 4
  • 3
  • +1
1 Solution
This seems like a simple question, but in order to understand more and ensure a correct answer, I will require a table def  (I am assuming you mean COLUMNS (A,B,C,D)?  Are there infact two tables or one table?  what is the key that associates A and B together?
johnsoneSenior Oracle DBACommented:
Seems like too simple an answer, but is this what you are looking for?

select a, b, count(1)
from t
group by a, b
having count(1) > 1;
XxtremeProAuthor Commented:
Thanks Jacobfw for trying to understand what I have been asking

Really like that question

Thanks johnsone the query worked but more correctly when I removed the last line
but still not quite the answer I wished to see

Answering Jacobfw, I will require a table def
 (I am assuming you mean COLUMNS (A,B,C,D)?  Are there infact two tables or one table?  what is the key that associates A and B together?
Yes Coloumn's A, B ,C, D

Only one Table

Key that associates A and B together ... Not sure as I have not created the table.. but both are primary key with an other field C as primary key

Hope I was clear

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

XxtremeProAuthor Commented:
select A, B, count(2)
from T
group by A, B

got some output not sure about the results

Please help me with the explanation as I am not sure I interpret the results properly

johnsoneSenior Oracle DBACommented:
The having was so that you would only see records where there was more than one.  Do you want to see records where there are only 1?  Then what you have posted should give you the count you are looking for.

If these are not correct, can you post some sample data and your expected output?
XxtremeProAuthor Commented:
I was expecting output in this way

A( 1st coloumn )                               B( 2nd Column)                         Count of B ( Showing A has got 2 or 3 or how many its got)
--------------------                               -------------------                            -------------------------------------------------------------------------
xyz                                                            1                                                                          4 ( 4 Records are shown against xyz)
<now the next record with the list of records showing against the record in Column A 1st Column)>

I was thinking I have shown the same sort of output when I initially posted
johnsoneSenior Oracle DBACommented:
This should do what you are looking for:

select s.a, t.b, s.cnt
from t, (select a, count(1) cnt from t group by a) s
where s.a=t.a order by s.a, t.b;

It will repeat the first and third columns, but I believe it gives you the results you are looking for.
slightwv (䄆 Netminder) Commented:
Try this little trick posted by another Expert from:

drop table tab1 purge;
create table tab1(a number, b number);
insert into tab1 values(1,12);
insert into tab1 values(1,13);
insert into tab1 values(1,14);
insert into tab1 values(1,15);
insert into tab1 values(2,0);
insert into tab1 values(3,23);
insert into tab1 values(3,24);
insert into tab1 values(3,25);


select decode(myrownum,1,a) a,
		decode(myrownum,1,mycount) mycount
select a, b, count(1) over(partition by a) mycount, row_number() over(partition by a order by a, b) myrownum
from tab1

Open in new window

johnsoneSenior Oracle DBACommented:
Wow.  That is a really cool trick.  I'll have to remember that one.
slightwv (䄆 Netminder) Commented:
It is, isn't it.  I loved it!  It was immediately added to my EE knowledgebase.
XxtremeProAuthor Commented:
thank you. solved the problem
slightwv (䄆 Netminder) Commented:
Can I ask why you selected the answer you did after you posted "but more correctly when I removed the last line
but still not quite the answer I wished to see"
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now