Zergman Posted December 12, 2008 Share Posted December 12, 2008 Im really not sure how to describe this so please bare with me on this. I have a column in the database that stores our employee ID's whenever a new incident is submitted to the database. What I was asked is to have a display of the top 5 ID's that call in the most. So what I need is a query that will calculate the amount of ID's are in the database, count how many times each one called in and display the top 5 highest amount of ID's. .... hope I explained that right. Don't even know where to start with this. Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/ Share on other sites More sharing options...
fenway Posted December 12, 2008 Share Posted December 12, 2008 Why not a COUNT() with GROUP BY? Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/#findComment-714192 Share on other sites More sharing options...
Maq Posted December 12, 2008 Share Posted December 12, 2008 Something like this perhaps? Not tested* SELECT COUNT(id), id FROM table ORDER BY COUNT(id) DESC LIMIT 5; Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/#findComment-714203 Share on other sites More sharing options...
corbin Posted December 12, 2008 Share Posted December 12, 2008 Something like this perhaps? Not tested* SELECT COUNT(id), id FROM table ORDER BY COUNT(id) DESC LIMIT 5; Errrrr....? Huh? Did you mean to throw a GROUP BY in there? Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/#findComment-714232 Share on other sites More sharing options...
Zergman Posted December 12, 2008 Author Share Posted December 12, 2008 I tried the example using GROUP BY and when I echo the query, I get Resource id #130 Here's the one in the page SELECT COUNT(flagentTID), flagentTID FROM data GROUP BY (flagentTID) DESC LIMIT 5 Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/#findComment-714254 Share on other sites More sharing options...
Zergman Posted December 12, 2008 Author Share Posted December 12, 2008 Hitting the gigerweb modified the query a bit but oddly getting the same result when I echo it SELECT COUNT(flagentTID) FROM data GROUP BY flagentTID DESC LIMIT 5 Gives me Resource id #130 Resource id #130 Resource id #130 Resource id #130 Resource id #130 Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/#findComment-714263 Share on other sites More sharing options...
Zergman Posted December 12, 2008 Author Share Posted December 12, 2008 Researching Resource id messages, it looks like its the way im trying to output it on the page. I know a simple echo won't work, whats the best way? Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/#findComment-714273 Share on other sites More sharing options...
Barand Posted December 13, 2008 Share Posted December 13, 2008 $sql = "SELECT flagentTID, COUNT(*) as total FROM data GROUP BY flagentTID ORDER BY total DESC LIMIT 5"; $res = mysql_query($sql); while (list($id, $tot) = mysql_fetch_row($res)) { echo "$id : $tot <br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/#findComment-714382 Share on other sites More sharing options...
Zergman Posted December 13, 2008 Author Share Posted December 13, 2008 Perfect, works like a charm! Thansk for the example Barand! Quote Link to comment https://forums.phpfreaks.com/topic/136749-solved-help-with-a-calculation-query/#findComment-714793 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.