Friday, April 29, 2011

max count together in an sql query

I have the following table (simplified) in an oracle db:

productId  |  modelDescription 
         1 |             thing
         2 |     another thing
         3 |       not a thing
         4 |             thing

I want to select the modeldescription which has the highest appearence in this table. The problem is that there can be nearly infinite model descriptions. So the resultset should look like something like this:

modelDescription | appearance 
           thing |          2 
   another thing |          1 
             ... |        ...
From stackoverflow
  • select modeldescription, count(modeldescription) 
    from products 
    group by modeldescription
    order by 2 desc
    
    Red33mer : thaks thats exactly what i was looking for
  • In addition, if you only want the highest add the following:

    Select Top 1 modeldescription......

0 comments:

Post a Comment