Home » RDBMS Server » Server Administration » Getting n max rows
Getting n max rows [message #374996] Tue, 17 July 2001 21:13 Go to next message
Angelos Tantalos
Messages: 2
Registered: May 2001
Junior Member
I have the following query:

SELECT a.kod_ypal
,a.epon
,a.onom
,b.health_product_id
,b.name
,COUNT(b.health_product_id) COUNT

FROM V_YPAL_EIDIK a
,HIS_HEALTH_PRODUCTS b
,HIS_MED_PROC_PRESCRIPTS C
,HIS_MED_PROC_PRESCRIPTS_GRP d

WHERE a.kod_ypal = c.doc_doctor_id
AND c.med_proc_prescript_num = d.mpp_med_proc_prescript_num
AND d.hepr_health_product_id = b.health_product_id
AND c.type_flg = '1'
AND c.issue_date BETWEEN '1/1/1999' AND '1/1/2003'
AND a.kod_ypal BETWEEN '1' AND '99999'
GROUP BY a.kod_ypal,a.epon,a.onom,b.health_product_id,b.name

and i get the following result:

kod_ypal epon onom health_product_id name count
7320 Smith John 337 descr1 1
7320 Smith John 409 descr2 3
7320 Smith John 612 descr3 2
7321 Lou Nick 337 descr1 1
7321 Lou Nick 588 descr4 2

I want to select the n most 'occurent' rows,depending on count column, of each kod_ypal. Is there any way i can do it with SQL? e.g If i wanted to select the 2 most 'occurent' rows,my result set would be:

kod_ypal epon onom health_product_id name count
7320 Smith John 409 descr2 3
7320 Smith John 612 descr3 2
7321 Lou Nick 337 descr1 1
7321 Lou Nick 588 descr4 2

If i wanted the most 'occurent' row of each kod_ypal then i would want the following result set:

kod_ypal epon onom health_product_id name count
7320 Smith John 409 descr2 3
7321 Lou Nick 588 descr4 2

Thanx in adavance!
Re: Getting n max rows [message #375274 is a reply to message #374996] Sat, 18 August 2001 07:30 Go to previous message
Santonu Mukherjee
Messages: 1
Registered: August 2001
Junior Member
i am not clear about the question u asked.. sorry for that, but i think i can help u..if u please clearify the question with simple example not the above on..my mail id is mail2_san@yahoo.com
thank u.
Previous Topic: How to set a conditional DEFAULT value to a column?
Next Topic: Re: simple update statement
Goto Forum:
  


Current Time: Tue Dec 24 10:30:49 CST 2024