Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count row numbers

Status
Not open for further replies.

henryz

Programmer
Oct 3, 2001
33
AU
Hi,

For example I have a simple table called Session like this:

PersonID SessionNum
========== =============
101 105
101 21
102 33
102 46
102 57
103 69
. .
. .
The SessionNum are unique, but in a random order.
Can someone suggest how to write a query, to find which person has the most occurance of the SessionNum, and how many? and which person has the less?

Thanks in advance,
-----
Henry
 

Try using the MAX(), MIN() aggregate functions. [3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Henry,

Here is some code, using your posted values:
Code:
col a heading “Person|at|Max/Min” format 999
col b heading “Max/Min|Sessions”
select Pers a, MaxMin_sess b
from (Select max(count(*)) MaxMin_sess from sess group by personid
      union
      Select min(count(*)) from sess group by personid) a
    ,(select personid pers,count(*) pers_cnt from sess
       group by personid) b
where b.pers_cnt = MaxMin_sess
/

 Person
     at    Max/Min
Max/Min   Sessions
------- ----------
    103          1
    102          3
Let us know if this approaches what you wanted.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[ Providing low-cost remote Database Admin services]
Click here to join Utah Oracle Users Group on Tek-Tips if you use Oracle in Utah USA.
 
Thanks Mufasa.

This solution looks pretty good, but our database is reasonable large, with over a million records. Tried this on our database, it just timed out without returning any results.

Appreciate your help anyway.
 
Henry,

Timeouts don't occur from lack of results reaching the screen...they happen from lack of activity. Reading millions of records is not lack of activity. IMHO, if you received a timeout, I believe there is another reason.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[ Providing low-cost remote Database Admin services]
Click here to join Utah Oracle Users Group on Tek-Tips if you use Oracle in Utah USA.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top