Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Sql queries

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-24 : 18:10:07
Thomas writes "I have a table of alarms.

I want a list of the number of occurrences of each type of alarm ordered by the number of occurrences.
Currently I:


set r = db.OpenRecordset("SELECT distinct(atxt) FROM alarms")
Do While Not r.EOF
set r2 = db.openrecordset("select count(*) as nrecs from alarms where atxt='" & r.Fields("atxt") & "'")
r.movenext
loop


This returns all the records and their number of occurrences but it now needs to be sorted in order of number of occurrences of each.

Is there a better way?

Thanks for help
Tom Ryan"

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2002-03-24 : 18:55:34
Hi Tom

Yes, there is a much easier and quicker way.


Select atxt, count(*) as nrecs
FROM alarms
GROUP BY atxt
ORDER BY count(*) DESC


Hope that helps

Damian
Go to Top of Page
   

- Advertisement -