V
V
Vladimir Kuznetsov2017-02-02 23:18:55
PHP
Vladimir Kuznetsov, 2017-02-02 23:18:55

How to find the maximum from sums with grouping?

There are three tables (figuratively):
1) checks (id, id_server, online, date_check, ...); // Checks
2) servers(id, id_project, title, ...); // Servers
3) projects(id, title, ...). // Projects

The essence of the problem is that in order to display statistics on the site, I need to display the information for the current month in a graph in which it is necessary to show the maximum online project (which should consist of the maximum online project servers) for each day of the month. Several checks are performed per day. I puzzle over the request, I tried through the nested ones, but I didn’t come to anything.
Briefly: get the sum of the maximum online servers of one project, grouped by days.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
ReenExe, 2017-02-03
@manullol

SELECT `projects`.`id`, `projects`.`title`, SUM(`CHECK_MAX`.`max_online`)
FROM `projects`
INNER JOIN `servers` ON (`servers`.`id_project` = `projects`.`id`)
INNER JOIN (
  SELECT
    `checks`.`id_server`,
    `checks`.`date_check`,
    MAX(`checks`.`online`) AS `max_online`
  FROM `checks`
  GROUP BY `checks`.`id_server`, `checks`.`date_check`
) AS `CHECK_MAX` ON (`CHECK_MAX`.`id_server` = `servers`.`id`)
GROUP BY `projects`.`id`, `CHECK_MAX`.`date_check`;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question