views:

148

answers:

1

I am creating a query to pull results from a MySQL database using PHP, the results of the query are output to an Excel document.

Here is the query:

$sql = "SELECT node, port, MAX(utiltx), MAX(utilrx), time, hour(time), day(time), month(time) FROM intfstats WHERE node IN('$separated') AND year(time)=$year AND month(time)=$month AND port IN('$portsep') GROUP BY node,port,day(time);";

I think the query works ok, but it always displays the time as midnight (which is first in the timestamp column).

The aim is for the product of the query to be the maximum utilisation rx and tx by day, and the time of day that the maximum throughput occured.

I would appreciate any help on this as I have spent far too long on it with not enough knowledge of MySQL queries to pull it off!!

A: 

How do you deal with tie-breakers, when you have 2+ records with the same max value on a given day?

SELECT t.node, 
       t.port, 
       t.utiltx, 
       t.utilrx, 
       t.time, 
       HOUR(t.time), 
       DAY(t.time), 
       MONTH(t.time) 
  FROM INTFSTATS t
  JOIN (SELECT is.node,
               is.port,
               MAX(is.utiltx) 'mutiltx', 
               MAX(is.utilrx) 'mutilrx',
               DAY(is.time) 'dd', 
               MONTH(is.time) 'mon',
               YEAR(is.time) 'yy'
          FROM INTFSTATS is
         WHERE is.node = $node
           AND is.port = $port
      GROUP BY is.node, 
               is.port, 
               DAY(is.time), 
               MONTH(is.time), 
               YEAR(is.time)) x ON x.node = t.node
                               AND x.port = t.port
                               AND x.mutiltx = t.utiltx
                               AND x.mutilrx = t.utilrx
                               AND x.dd = DAY(t.time)
                               AND x.mon = MONTH(t.time)
                               AND x.yy = YEAR(t.time)
 WHERE x.yy = $year
   AND x.mon = $mon
OMG Ponies
I found nested selects to be extremely slow in MySQL when dealing with hundreds of thousands of records.
If you have a different way to get the maximum value on a per widget, per day basis - I'm all ears. If the data model can't be changed to suit the needs, then maybe consider PostgreSQL?
OMG Ponies
Thanks very much for your quick answer :)Given the question on tie breakers, it's not something that I had considered in the query to be honest.I've been trying out the solution that you have offered and I'm sure it is nearly there but I am getting an error:"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'x ON x.node = t.node AND x.port = t.port ' at line 22"Do you have any idea what might be causing this?
daviiies
Ok ignore my last comment as I have worked out what was wrong - there was one missing closing bracket! The code now does run, although the final step is I need to integrate a couple of variables in there: $month, $year - these need to be selectable as otherwise too much data is pulled back. $node, $port also need to be in there as a user needs to be able to input the ones to query on a form. Any help with that appreciated and I will continue trying in the mean time!
daviiies
OMG Ponies