MySQL按类别和子类别排序

时间:2016-04-28 01:24:02

标签: mysql sql-order-by

我正在尝试在MySQL中对一些Nagios警报进行排序。

我希望将第2列组合在一起,就像现在一样。所有警报都已分组。问题是我希望这些组按降序排列。例如,“所有主机|所有事件| 65 |”应该在顶部,接下来将是“All Hosts | memory-usage-UNX | 25”,接下来是25个memory-usage-UNX警报。接下来是“所有主机| CPU-Load-UNX | 9”,然后根据计数以降序排列其余的CPU-Load-UNX警报。

这是我的查询。

SELECT  ifnull(Host,'All Hosts') as Host
           ,ifnull(Event,'All Events') as Event
       ,Event_Count
       FROM
       (
       SELECT  Host
        ,name2 as Event
       -- ,Occurrences
      ,count(name2) as Event_Count
      FROM
      (
      SELECT  ifnull(nbo.name1,'All Hosts') as Host
       ,nbo.name2
           ,count(1) as Occurrences
      FROM
        nagios_notifications no
      ,nagios_objects nbo
      WHERE
          no.object_id = nbo.object_id
      AND nbo.name1 in (select nh.display_name
                                   from  nagios_hostgroup_members nhm
                        ,nagios_hosts nh
                                   where nhm.host_object_id = nh.host_object_id
                     and nhm.hostgroup_id in (select hostgroup_id
                                              from nagios_hostgroups
                                              where alias in ('Production-and-Critical-IT')
                                              )
                   )
      AND no.start_time >= now() - interval 1 week
      AND no.state = 2  -- change to != 0 for everything that's not OK
      GROUP BY nbo.name1,nbo.name2 ASC WITH ROLLUP
      ) a
      -- where Host = 'All Hosts' or name2 is not null
      WHERE name2 is not null
      GROUP BY Event,Host with rollup
      ) b
      order by Event, Host, Event_Count;

这是输出。

+--------------------+------------------------------------------+-------------+
| Host               | Event                                    | Event_Count |
+--------------------+------------------------------------------+-------------+
| All Hosts          | All Events                               |          65 |
| All Hosts          | check_activesync-WIN                     |           1 |
| pexc001            | check_activesync-WIN                     |           1 |
| All Hosts          | check_asterisk_extension_error-TEL       |           1 |
| pteln004           | check_asterisk_extension_error-TEL       |           1 |
| All Hosts          | check_dag-WIN                            |           1 |
| pexc001            | check_dag-WIN                            |           1 |
| All Hosts          | check_exchange_mailqueue-WIN             |           1 |
| pexc001            | check_exchange_mailqueue-WIN             |           1 |
| All Hosts          | check_health_measure-JVA                 |           2 |
| pwspr031           | check_health_measure-JVA                 |           1 |
| pwspr049           | check_health_measure-JVA                 |           1 |
| All Hosts          | check_http_prod-JVA                      |           4 |
| putpr002           | check_http_prod-JVA                      |           1 |
| putpr004           | check_http_prod-JVA                      |           1 |
| pwspr027           | check_http_prod-JVA                      |           1 |
| pwspr031           | check_http_prod-JVA                      |           1 |
| All Hosts          | check_jmx_HeapMemoryUsage_background-JVA |           1 |
| putpr001           | check_jmx_HeapMemoryUsage_background-JVA |           1 |
| All Hosts          | check_tomcat-UNX                         |           1 |
| putpr002           | check_tomcat-UNX                         |           1 |
| All Hosts          | check_total_procs-UNX                    |           1 |
| naggy2             | check_total_procs-UNX                    |           1 |
| All Hosts          | check_w305_KC_standby-DBA                |           1 |
| pdbdrkc01          | check_w305_KC_standby-DBA                |           1 |
| All Hosts          | cisco-cpu-load-UNX                       |           1 |
| crrt01.pr1         | cisco-cpu-load-UNX                       |           1 |
| All Hosts          | CPU-Load-UNX                             |           9 |
| pfspr02            | CPU-Load-UNX                             |           1 |
| psearchpr005       | CPU-Load-UNX                             |           1 |
| psearchpr006       | CPU-Load-UNX                             |           1 |
| psearchpr007       | CPU-Load-UNX                             |           1 |
| psearchpr008       | CPU-Load-UNX                             |           1 |
| putpr001           | CPU-Load-UNX                             |           1 |
| putpr004           | CPU-Load-UNX                             |           1 |
| pwspr031           | CPU-Load-UNX                             |           1 |
| pwspr049           | CPU-Load-UNX                             |           1 |
| All Hosts          | disk-usage-UNX                           |           1 |
| pmsvpr007          | disk-usage-UNX                           |           1 |
| All Hosts          | memory-usage-UNX                         |          26 |
| p0crmpr001         | memory-usage-UNX                         |           1 |
| p0crmpr002         | memory-usage-UNX                         |           1 |
| pmsvpr002          | memory-usage-UNX                         |           1 |
| pmsvpr008          | memory-usage-UNX                         |           1 |
| putpr001           | memory-usage-UNX                         |           1 |
| pwspr013           | memory-usage-UNX                         |           1 |
| pwspr014           | memory-usage-UNX                         |           1 |
| pwspr019           | memory-usage-UNX                         |           1 |
| pwspr022           | memory-usage-UNX                         |           1 |
| pwspr024           | memory-usage-UNX                         |           1 |
| pwspr025           | memory-usage-UNX                         |           1 |
| pwspr026           | memory-usage-UNX                         |           1 |
| pwspr027           | memory-usage-UNX                         |           1 |
| pwspr030           | memory-usage-UNX                         |           1 |
| pwspr031           | memory-usage-UNX                         |           1 |
| pwspr032           | memory-usage-UNX                         |           1 |
| pwspr033           | memory-usage-UNX                         |           1 |
| pwspr036           | memory-usage-UNX                         |           1 |
| pwspr041           | memory-usage-UNX                         |           1 |
| pwspr042           | memory-usage-UNX                         |           1 |
| pwspr043           | memory-usage-UNX                         |           1 |
| pwspr044           | memory-usage-UNX                         |           1 |
| pwspr045           | memory-usage-UNX                         |           1 |
| pwspr046           | memory-usage-UNX                         |           1 |
| pwspr049           | memory-usage-UNX                         |           1 |
| pwspr050           | memory-usage-UNX                         |           1 |
| All Hosts          | new_pending_phone-TEL                    |           1 |
| pdbpr01            | new_pending_phone-TEL                    |           1 |
| All Hosts          | prod-best-match-JVA                      |           1 |
| Prod-MicroServices | prod-best-match-JVA                      |           1 |
| All Hosts          | prod-consumer-rating-rewards-JVA         |           1 |
| Prod-MicroServices | prod-consumer-rating-rewards-JVA         |           1 |
| All Hosts          | prod-credit-request-JVA                  |           1 |
| Prod-MicroServices | prod-credit-request-JVA                  |           1 |
| All Hosts          | prod-favorite-pros-JVA                   |           1 |
| Prod-MicroServices | prod-favorite-pros-JVA                   |           1 |
| All Hosts          | prod-ha-spd-update-JVA                   |           1 |
| Prod-MicroServices | prod-ha-spd-update-JVA                   |           1 |
| All Hosts          | prod-payment-batch-JVA                   |           1 |
| Prod-MicroServices | prod-payment-batch-JVA                   |           1 |
| All Hosts          | prod-session-log-loader-JVA              |           1 |
| Prod-MicroServices | prod-session-log-loader-JVA              |           1 |
| All Hosts          | prod-sm-caldav-event-JVA                 |           1 |
| Prod-MicroServices | prod-sm-caldav-event-JVA                 |           1 |
| All Hosts          | prod-sp-task-coverage-indexer-JVA        |           1 |
| Prod-MicroServices | prod-sp-task-coverage-indexer-JVA        |           1 |
| All Hosts          | prod-watcher-JVA                         |           1 |
| Prod-MicroServices | prod-watcher-JVA                         |           1 |
| All Hosts          | prod-ws-api-security-JVA                 |           1 |
| Prod-MicroServices | prod-ws-api-security-JVA                 |           1 |
| All Hosts          | prod-ws-communication-JVA                |           1 |
| Prod-MicroServices | prod-ws-communication-JVA                |           1 |
| All Hosts          | prod-ws-entity-JVA                       |           1 |
| Prod-MicroServices | prod-ws-entity-JVA                       |           1 |
+--------------------+------------------------------------------+-------------+

1 个答案:

答案 0 :(得分:0)

在查询结束时,请确保在order by子句

中指定顺序
Order by host asc, event asc, event_count desc