Forum Moderators: open

Message Too Old, No Replies

get records on the basis of their occurences

         

ayushchd

10:33 am on Nov 24, 2007 (gmt 0)

10+ Year Member



Hi All.

Suppose I have in a table :

id : msg_id
1 : 1
2 : 2
3 : 1
4 : 1
5 : 3
6 : 2
7 : 3
8 : 3
9 : 1
10 : 1

A query that will give me the msg_ids in decreasing order of their number of occurrences.
For above example, it should give :
1, 3 and 2

phranque

11:34 am on Nov 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



SELECT DISTINCT(msg_id) FROM msg_table_name GROUP BY msg_id ORDER BY COUNT (msg_id) DESC

ayushchd

11:51 am on Nov 24, 2007 (gmt 0)

10+ Year Member



it says invalid use of group function

phranque

12:01 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



perhaps it is unnecessary.
try this:
SELECT DISTINCT(msg_id) FROM msg_table_name ORDER BY COUNT(msg_id) DESC

ayushchd

2:37 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



still says :
#1111 - Invalid use of group function

ayushchd

3:00 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



This Worked!

SELECT DISTINCT (msg_id), COUNT( msg_id ) AS msgcount FROM discussions
GROUP BY msg_id ORDER BY msgcount DESC

ZydoSEO

7:39 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The GROUP BY should make it distinct... So a slightly simplified version of the above should be:

SELECT msg_id, COUNT(msg_id) AS msgcount
FROM discussions
GROUP BY msg_id
ORDER BY msgcount DESC

Haven't tried it but this should also work.