Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by - multiple conditions - MySQL

Tags:

database

mysql

How can I combine 2 group by conditions? I have records for each id for every hour in a day and I want to group information by first id and all records for that id in that day then second id and all records for that in the day.

My sample query is this:

SELECT
    r.name
  , r.network
  , r.namestring
  , i.name
, i.description
  , r.rid
  , i.id
  , d.dtime
  , d.ifInOctets
FROM router AS r
INNER JOIN interface AS i
ON r.rid = i.rid
INNER JOIN 1279080000_1_60 AS d
ON i.id = d.id
AND dtime BETWEEN 1279113600 AND 1279115400
WHERE r.network = "ITPN"
AND i.status = "active"
GROUP BY i.id AND d.dtime              // each id with all its dtime

This always ends up giving me an aggregate value for that id. Any idea what I could use??? I don't want to sum up all values.

Thank you,

like image 505
JJunior Avatar asked Jan 22 '23 14:01

JJunior


1 Answers

To group by multiple expressions you should separate them with a comma, not AND:

GROUP BY i.id, d.dtime

You should also ensure that two records form the same day have the same value of dtime. It's not clear from your question whether this is or is not the case.

like image 197
Mark Byers Avatar answered Jan 26 '23 00:01

Mark Byers