Big_Pat Posted May 10, 2013 Share Posted May 10, 2013 Hi, I'm trying to figure out the following. I know it must need a nested loop, I just can't figure out quite the way to do it, so any help would be superb. My MySQL db has a list of incidents that a servicedesk team take in any given month. It outputs, among others, incident_number, logged_by and resolved_by. The logged_by is, of course, an individual member of the servicedesk. That person MAY resolve the incident himself, so he MAY appear in the resolved_by field too. My aim is to get the following: SD member 1, calls logged, calls resolved. SD member 2, calls logged, calls resolved. etc Can someone help, please? Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 10, 2013 Share Posted May 10, 2013 With what? You've posted no code, no description of what you've tried... Quote Link to comment Share on other sites More sharing options...
Big_Pat Posted May 10, 2013 Author Share Posted May 10, 2013 Oh, sorry. Well, I got a bit confused in my nested loops, you see, so it wasn't making a lot of sense. Basically, I started with this but got lost. $countlogged=count($logged_by); $countresolved=count($resolved_by); for ($i=0; $i<$countlogged;$i++){ for ($j=0;$j<$countresolved;$j++){ if ($resolved_by[$j] == $logged_by[$i]){ $resolved_sum[$i]= $resolved_sum[$i] +1; } $logged_sum[$i] = $logged_sum[$i] +1; } //closes loop of resolvedbys }//closes loop of loggedbys for ($i=0; $i<$countlogged;$i++){ echo $logged_sum[$i] . " - " . $resolved_sum[$i] ."<br />"; } Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted May 10, 2013 Solution Share Posted May 10, 2013 (edited) If the data is in a database, you can just calculate the values using a query. SELECT member, SUM(logged) as logged_count, SUM(resolved) as resolved_count FROM ( SELECT logged_by as member, COUNT(incident_number) as logged, 0 as resolved FROM table_name GROUP BY logged_by UNION SELECT resolved_by as member, 0 as logged, COUNT(incident_number) as resolved FROM table_name GROUP BY resolved_by ) as results GROUP BY member EDIT: Corrected query Edited May 10, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Big_Pat Posted May 10, 2013 Author Share Posted May 10, 2013 Fantastic, thanks Psycho! I'd moved on to using $countlogged=count(array_unique($logged_by)); and was *almost* there, but your query worked a charm. I'd never have got that. My MySQL queries are only on the basic level. Quote Link to comment 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.