I need to select some values, group them and order by multiple fields. Here is my fiddle: http://sqlfiddle.com/#!2/a80eb/3
What I need to achieve, is to select one row from the table packet_data for a given value of client_mac and for each distinct item in the column drone_id. This row should contain the value of client_mac, drone_id and the most frequent value of the antenna_signal column for the given combination of drone_id and client_mac.
The column drone_id is a foreign key into the table drones and there is a column map_id in this table. I need to concern only those rows from the packet_data table which have a certain map_id in the drones table.
My desired result should be this:
CLIENT_MAC DRONE_ID ANTENNA_SIGNAL
3c:77:e6:17:9d:1b 1 -37
3c:77:e6:17:9d:1b 2 -57
My current SQL query is:
SELECT `packet_data`.`client_mac`,
`packet_data`.`drone_id`,
`packet_data`.`antenna_signal`,
count(*) AS `count`
FROM `packet_data`
JOIN `drones` ON `packet_data`.`drone_id`=`drones`.`custom_id`
WHERE `drones`.`map_id` = 11
AND `client_mac`="3c:77:e6:17:9d:1b"
GROUP BY drone_id,
`packet_data`.`antenna_signal`
ORDER BY `packet_data`.`drone_id`,
count(*) DESC
And my current result:
CLIENT_MAC DRONE_ID ANTENNA_SIGNAL
3c:77:e6:17:9d:1b 1 -37
3c:77:e6:17:9d:1b 1 -36
3c:77:e6:17:9d:1b 2 -57
3c:77:e6:17:9d:1b 2 -56