0

Cant seem to figure this one out...

I have a table, one of the fields is variabletype. There are several user input variable types. For example:

id | variabletype
 1 | button
 2 | text
 3 | button
 4 | link
 5 | button
 6 | link

I wrote some SQL to basically count the number of times each variable type is listed. I nestled that into a subquery so that I can then get the record that has the max number of instances (in this example - button).

My problem is the query only returns the max number, it does not display the actual variable type. my ideal outcome is having the variabletype display along with the max count. Any thoughts?

 SELECT MAX(y.mosttested)
   FROM (SELECT variabletype, COUNT(variableid) AS mosttested
     FROM variable GROUP BY variabletype) y
4

3 回答 3

1

Try this :

SELECT `variabletype `,
       COUNT(`variabletype `) AS `value_occurrence` 
FROM   `my_table`
GROUP BY `value`
ORDER BY `value_occurrence` DESC
LIMIT    1;
于 2013-10-12T22:43:11.293 回答
0

Try

select variabletype,n
from 
  (SELECT variabletype, COUNT(variableid) AS mosttested
  FROM variable GROUP BY variabletype) v 
where mosttested=(SELECT MAX(y.mosttested) AS n
   FROM (SELECT variabletype, COUNT(variableid) AS mosttested
     FROM variable GROUP BY variabletype) y)
于 2013-10-12T22:43:46.233 回答
0

If row_number function is avialable in your SQL environment then you can do the following:

SELECT y.variabletype, y.mosttested as max_mosttested
   FROM (SELECT variabletype, COUNT(variableid) AS mosttested, row_number()over(order by count(variableid) desc)
     FROM variable GROUP BY variabletype) y
where rownum=1
于 2013-10-12T22:49:50.567 回答