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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Top 5 of each catagory

Status
Not open for further replies.

Zipster

Programmer
Nov 13, 2000
107
GB
Hi,

I have my data structured as follows:

nID txtName txtType boolShow

0 Ted CAR 1
1 Sam CAR 1
2 Kevin VAN 1
3 Sarah VAN 1
And so on...

How would I show any top 5 of each txtType where the boolShow value is true?

Thanks for your help.

K Abbott IT Manager / Web Developer

 
i don;t think sql can do "any top 5"

"top" makes sense only if you specify which top 5, based on the values in some column(s)

"any" suggests randomization

and finally, but most importantly, which database system are you using?



r937.com | rudy.ca
 
like this --

SELECT TOP 5 myCol FROM myTable

you are likely to get the "nearest" 5, not the "top" 5

r937.com | rudy.ca
 
That will do, but I need the top five of each txtType like the data structure in my first post.

Do you know whow I would do this?

Ta.

K Abbott IT Manager / Web Developer

 
yes, i know how to get the top 5 by category, but like i said, unfortunately it requires some distinguishing criterion in order to write the sql

how about the top 5 by nid? the top 5 by txtname?

actually, there aren't that many alternatives, unless of course your table is really more complex than what you have shown

r937.com | rudy.ca
 
Code:
select t1.nID
     , t1.txtName
     , t1.txtType
  from yourtable as t1
inner
  join yourtable as t2
    on t2.txtType = t1.txtType
   and t2.txtName >= t1.txtName
group
    by t1.nID
     , t1.txtName
     , t1.txtType
having count(*) <= 5

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top