tags:

views:

47

answers:

3

I have a mysql database with records from different companies. I need to select records from companies which have the most, second most and third most records and plot their number of records per year. How do I select them?

Many thanks.

EDIT:

The table would look something like this:

Company    Year
A          1999
A          1999
B          1999
C          1999
A          2000
C          2000
A          2003

So if I select the company with the most records, A has the most records, and the output is;

Year    Total
1999     2
2000     1
2003     1

And for the company with second most records, the output is ( in this case, company C)

Year    Total
1999     1
2000     1

Third most will be company B.

+3  A: 

I'd say something like

SELECT company,COUNT(company) AS rec,year
FROM your_table GROUP BY company, year ORDER BY rec DESC LIMIT 3;
gregseth
Ha! u beat me to it!
slashmais
hmm.. how do I select only the company with the most records, to draw a graph chart of it's record growth over the year? and also, the second most records, etc
they have to selected separately; thanks for your help
Run the query above, return the data, and then for the top three results, run a select * (although obviously not using *) against that company id...
ZombieSheep
can that be automated? I would like to put the query into a php script and have the script produce the relevant charts immediately (seperate scripts for most, 2nd and 3rd)
use the SQL to get the data you need, then use each row of data as input to your charting script to produce the 3 charts you need.
poh
i tried the SQL, it gave something like:Company | rec |yearA | 5 | 2008B | 10 | 2008C | 2 | 2008I need it to determine which company has the most, 2nd most, and 3rd most data, then produce a trend data which show , for each company, the number of records per year.
A: 

Something like this could do the trick:

select recordcount from companies order by recordcount DESC limit 3;

slashmais
A: 

most_frequent:

SELECT year, COUNT(year) AS total FROM your_table 
WHERE company = 
    (SELECT company, COUNT(company) AS c
     FROM your_table
     GROUP BY c
     ORDER BY c DESC
     LIMIT 0,1)

second most:

SELECT year, COUNT(year) AS total FROM your_table 
WHERE company = 
    (SELECT company, COUNT(company) AS c
     FROM your_table
     GROUP BY c
     ORDER BY c DESC
     LIMIT 1,1)

third most:

SELECT year, COUNT(year) AS total FROM your_table 
WHERE company = 
    (SELECT company, COUNT(company) AS c
     FROM your_table
     GROUP BY c
     ORDER BY c DESC
     LIMIT 2,1)

In order to get two dimensional output in one query:

SELECT company, year, COUNT(year) AS total FROM your_table 
INNER JOIN
    (SELECT company, COUNT(company) AS c
     FROM your_table
     GROUP BY c
     ORDER BY c DESC) AS t1
ON your_table.company = t1.company
ORDER BY t1.c DESC

I'm sorry, I haven't tested it. Just leave a comment if you have any trouble.

Martin
looks good; i did a query to get the top 3, by using limit 0,3, putting them into an array, then running SELECT based on the elements in the array for all the three.