| Author |
Topic |
|
Mannga
Yak Posting Veteran
70 Posts |
Posted - 2002-01-08 : 10:25:47
|
Hi All,I am trying to create a stored procedure at the moment that will return to me the 20 most frequent records.My database looks something like this.[ID] [OrgName] [other fields ]Now if I run a query something likeSELECT DISTINCT(OrgName) FROM tableI get 647 records returned.Now what I would like to do is return the 20 distinct records which are used more if you get my meaning?I hope that makes sense to someone out there?ThanksGavin |
|
|
smccreadie
Aged Yak Warrior
505 Posts |
Posted - 2002-01-08 : 10:30:33
|
| How about SELECT TOP 20 OrgName, COUNT(ID)FROM tableGROUP BY OrgnameORDER BY COUNT(ID) DESC |
 |
|
|
Nazim
A custom title
1408 Posts |
Posted - 2002-01-08 : 10:32:21
|
| Do you a field which will keep the count of times the record is used?if so,select top 20 distinct distinct id, countfieldfrom tablenamewhere countfield>20order by countfieldDo you have a field which updates its last usage(if you searching for the most 20 last used)?select top 20 distinct distinct id, lastuseddatetimefrom tablenamewhere lastuseddatetime>20How will you decide on how many times the record is used, OR are you adding that record with the ID again in the table on its use. there by a record with ID 10 if it has 30 records in the table, it is as good as saying 30 times it is used?.select id, count(*) from tablenamegroup by idhaving count(*)>20HTH----------------------------Anything that Doesn't Kills you Makes you Stronger |
 |
|
|
Mannga
Yak Posting Veteran
70 Posts |
Posted - 2002-01-08 : 10:40:33
|
quote: How about SELECT TOP 20 OrgName, COUNT(ID)FROM tableGROUP BY OrgnameORDER BY COUNT(ID) DESC
Woohoo .... works perfectly, thanks a lot |
 |
|
|
Mannga
Yak Posting Veteran
70 Posts |
Posted - 2002-01-08 : 11:01:03
|
Just one more question on this one ...How can I get my results to be in alphabetical order?What I would like is now that I have my top 20, for them now to be sorted alphabetically....Thanks,Gavin |
 |
|
|
Nazim
A custom title
1408 Posts |
Posted - 2002-01-08 : 11:10:19
|
| Select * from(SELECT TOP 20 OrgName, COUNT(ID)FROM tableGROUP BY OrgnameORDER BY COUNT(ID) DESC) aorder by a.OrgName----------------------------Anything that Doesn't Kills you Makes you Stronger |
 |
|
|
Mannga
Yak Posting Veteran
70 Posts |
Posted - 2002-01-08 : 11:24:32
|
| When i try to save this procedure I get the error message'No Column was specified for column 2 of a'And if I change the procedure to thisSelect * from(SELECT TOP 20 OrgName, COUNT(ID)FROM tableGROUP BY OrgnameORDER BY COUNT(ID) DESC) a, border by a.OrgNameI can then save the procedure but I get this error message when I run it'Invalid object name 'b'.'??? |
 |
|
|
JustinBigelow
SQL Gigolo
1157 Posts |
Posted - 2002-01-08 : 11:41:17
|
| You need to give a name to your aggregated column...Select * from(SELECT TOP 20 OrgName, COUNT(ID) as "topcount"FROM tableGROUP BY OrgnameORDER BY COUNT(ID) DESC) aorder by a.OrgNamehth,Justin |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2002-01-08 : 11:42:12
|
| i think count(*) needs to be named....ie select countxyz = count(*) from tablexyzre the a,b....the 'a' is an alias for the resultset (aka derived table) produced in the subquery...therefore in your example 'b' doesn't exist as a table..... |
 |
|
|
Mannga
Yak Posting Veteran
70 Posts |
Posted - 2002-01-08 : 11:44:05
|
Cool, That is exactly what I was looking for,Thanks to everyone who has helped here..Cheers,Gavin |
 |
|
|
|