Postgresql - 在HAVING子句中的结果列上使用AVG

时间:2016-04-28 10:00:16

标签: postgresql aggregate-functions having

我对Postgresql(以及SQL本身)相当新,所以请原谅我,如果我错过了某些东西(是的,我确实尝试过使用搜索)。

我试图使用HAVING子句中查询的结果列的平均值。可能吗?或者有更好的解决方案吗?

我希望Alias' Sent_traffic'仅显示值大于或等于该列平均值的行。

以下查询是我想要做的(因为HAVING子句而无法工作):

    select p.name, SUM(f.bytes_ab) as Sent_traffic
    from pcap_flow f
    inner join ports p
    ON f.source_ip = p.source_ip AND f.source_port=p.source_port AND p.destination_ip=f.destination_ip AND p.destination_port=f.destination_port
    WHERE p.logged_at BETWEEN f.first_packet AND f.last_packet
    AND p.logged_at BETWEEN '2016-03-15' AND '2016-04-01'
    AND p.name!=''
    GROUP BY p.name
    HAVING SUM(f.bytes_ba) >= AVG(Sent_traffic)
    ORDER BY Sent DESC
    LIMIT 10;

在HAVING子句中没有AVG(已发送)的同一查询是什么工作:

    select p.name, SUM(f.bytes_ab) as Sent_traffic
    from pcap_flow f
    inner join ports p
    ON f.source_ip = p.source_ip AND f.source_port=p.source_port AND p.destination_ip=f.destination_ip AND p.destination_port=f.destination_port
    WHERE p.logged_at BETWEEN f.first_packet AND f.last_packet
    AND p.logged_at BETWEEN '2016-03-15' AND '2016-04-01'
    AND p.name!=''
    GROUP BY p.name
    HAVING SUM(f.bytes_ba) >= 999999
    ORDER BY Sent DESC
    LIMIT 10;

表的快照如下:

表pcap_flow: pcap_flow table in PgAdmin

表端口:

ports table in PgAdmin

pcap_flow表:

      id   | pcap_id | flow_code |         source_ip         | source_port            |      destination_ip       | destination_port | protocol |        first_packet        |        last_packet         |          status          |  total_time  | total_packets | idle_time_ab | idle_time_ba | bytes_ab | bytes_ba
    -------+---------+-----------+---------------------------+-------------+---------------------------+------------------+----------+----------------------------+----------------------------+--------------------------+--------------+---------------+--------------+--------------+----------+----------
         1 |      42 | a2b       | 192.168.0.22              |       50191 | 128.93.101.81             |              443 | T        | 2016-03-25 09:43:46.184039 | 2016-03-25 09:43:55.950184 | reset                    |     9.766144 |            10 |       9.7053 |       9.7065 |      510 |     3601
         2 |      42 | c2d       | 192.168.0.22              |       50127 | 74.125.133.189            |              443 | T        | 2016-03-25 09:43:46.212468 | 2016-03-25 09:44:04.860872 | reset (syns 0)  (fins 1) |    18.648403 |             6 |              |              |        0 |        0
         3 |      42 | e2f       | 192.168.0.22              |       50194 | 192.168.0.254             |               80 | T        | 2016-03-25 09:43:46.302105 | 2016-03-25 09:43:49.615557 | yes                      |     3.313451 |            10 |       1.7933 |       2.0006 |      336 |     1421
         4 |      42 | g2h       | 192.168.0.22              |       50196 | 104.16.27.216             |               80 | T        | 2016-03-25 09:43:46.335128 | 2016-03-25 09:43:46.454677 | yes                      |     0.119549 |             5 |       0.1172 |       0.1162 |      236 |     1705
         5 |      42 | i2j       | 192.168.0.254             |         443 | 192.168.0.22              |            50190 | T        | 2016-03-25 09:43:46.420872 | 2016-03-25 09:43:46.422176 | no (syns 0)  (fins 2)    |     0.001304 |             6 |       0.0012 |       0.0002 |     2063 |       74
         6 |      42 | k2l       | 192.168.0.22              |       50197 | 192.168.0.254             |              443 | T        | 2016-03-25 09:43:46.457142 | 2016-03-25 09:43:57.94442  | yes                      |    11.487277 |            26 |        2.001 |        2.001 |     3678 |    18859
         7 |      42 | m2n       | 192.168.0.22              |       50170 | 192.168.0.254             |              443 | T        | 2016-03-25 09:43:46.509135 | 2016-03-25 09:43:46.51023  | reset (syns 0)  (fins 2) |     0.001095 |             2 |        0.001 |       0.0001 |        0 |       37
         8 |      42 | o2p       | 192.168.0.22              |       50161 | 54.149.211.23             |              443 | T        | 2016-03-25 09:43:46.510764 | 2016-03-25 09:43:46.512014 | reset (syns 0)  (fins 2) |      0.00125 |             3 |       0.0011 |            0 |       37 |       37
         9 |      42 | q2r       | 192.168.0.22              |       50198 | 192.168.0.254             |              443 | T        | 2016-03-25 09:43:46.511504 | 2016-03-25 09:43:46.744645 | reset                    |     0.233141 |             7 |        0.138 |       0.0981 |      385 |     4342
        10 |      42 | s2t       | 192.168.0.22              |       50199 | 192.168.0.254             |              443 | T        | 2016-03-25 09:43:46.511667 | 2016-03-25 09:43:46.962999 | reset                    |     0.451332 |             8 |       0.2478 |       0.2018 |      385 |     4342
        11 |      42 | u2v       | 192.168.0.22              |       50200 | 104.16.27.216             |               80 | T        | 2016-03-25 09:43:46.600772 | 2016-03-25 09:43:46.776045 | yes                      |     0.175273 |             5 |        0.166 |       0.1648 |      463 |     1604
        12 |      42 | w2x       | 192.168.0.22              |       50201 | 104.16.27.216             |               80 | T        | 2016-03-25 09:43:46.606515 | 2016-03-25 09:43:46.760278 | yes                      |     0.153763 |             6 |       0.1517 |       0.1504 |      463 |     1604
        13 |      42 | y2z       | 192.168.0.22              |       50163 | 172.217.16.78             |              443 | T        | 2016-03-25 09:43:46.744559 | 2016-03-25 09:43:46.746244 | reset (syns 0)  (fins 2) |     0.001685 |             3 |       0.0014 |       0.0001 |       37 |       37
        14 |      42 | aa2ab     | 192.168.0.22              |       50202 | 192.168.0.254             |              443 | T        | 2016-03-25 09:43:46.763646 | 2016-03-25 09:43:48.673286 | reset                    |      1.90964 |             8 |       1.5839 |        1.789 |      397 |     4342
        15 |      42 | ac2ad     | 192.168.0.22              |       50203 | 104.16.27.216             |               80 | T        | 2016-03-25 09:43:46.854744 | 2016-03-25 09:43:46.998117 | yes                      |     0.143373 |             5 |       0.1414 |       0.1401 |      463 |     1604

ports表:

       id   | session_id |  pid  |          name           | protocol |         source_ip         |      destination_ip       | source_port | destination_port | state |        logged_at
    --------+------------+-------+-------------------------+----------+---------------------------+---------------------------+-------------+------------------+-------+-------------------------
          1 |          1 |   676 | svchost.exe             |       17 | 0.0.0.0                   |                           |          68 |                  |     2 | 2016-03-16 09:41:04.716
          2 |          1 |     4 |                         |       17 | 192.168.0.22              |                           |         137 |                  |     2 | 2016-03-16 09:41:04.716
          3 |          1 |     4 |                         |       17 | 192.168.0.22              |                           |         138 |                  |     2 | 2016-03-16 09:41:04.716
          4 |          1 |  3408 | svchost.exe             |       17 | 127.0.0.1                 |                           |        1900 |                  |     2 | 2016-03-16 09:41:04.716
          5 |          1 |  3408 | svchost.exe             |       17 | 192.168.0.22              |                           |        1900 |                  |     2 | 2016-03-16 09:41:04.716
          6 |          1 |  3092 | uoipservice.exe         |       17 | 192.168.0.22              |                           |        1900 |                  |     2 | 2016-03-16 09:41:04.716
          7 |          1 |  2208 | mdnsresponder.exe       |       17 | 192.168.0.22              |                           |        5353 |                  |     2 | 2016-03-16 09:41:04.716
          8 |          1 |  1032 | svchost.exe             |       17 | 0.0.0.0                   |                           |        5355 |                  |     2 | 2016-03-16 09:41:04.716
          9 |          1 |  2208 | mdnsresponder.exe       |       17 | 0.0.0.0                   |                           |       49152 |                  |     2 | 2016-03-16 09:41:04.716
         10 |          1 |  3092 | uoipservice.exe         |       17 | 192.168.0.22              |                           |       51128 |                  |     2 | 2016-03-16 09:41:04.716
         11 |          1 |  3092 | uoipservice.exe         |       17 | 192.168.0.22              |                           |       51129 |                  |     2 | 2016-03-16 09:41:04.716
         12 |          1 |  3408 | svchost.exe             |       17 | 192.168.0.22              |                           |       61182 |                  |     2 | 2016-03-16 09:41:04.716
         13 |          1 |  3408 | svchost.exe             |       17 | 127.0.0.1                 |                           |       61183 |                  |     2 | 2016-03-16 09:41:04.716
         14 |          1 |   676 | svchost.exe             |       17 | fe80::1d77:665c:b2e5:3be3 |                           |         546 |                  |     2 | 2016-03-16 09:41:04.716
         15 |          1 |  3408 | svchost.exe             |       17 | ::1                       |                           |        1900 |                  |     2 | 2016-03-16 09:41:04.716
         16 |          1 |  3408 | svchost.exe             |       17 | fe80::1d77:665c:b2e5:3be3 |                           |        1900 |                  |     2 | 2016-03-16 09:41:04.716
         17 |          1 |  2208 | mdnsresponder.exe       |       17 | ::1                       |                           |        5353 |                  |     2 | 2016-03-16 09:41:04.716
         18 |          1 |  1032 | svchost.exe             |       17 | ::                        |                           |        5355 |                  |     2 | 2016-03-16 09:41:04.716
         19 |          1 |  2208 | mdnsresponder.exe       |       17 | ::                        |                           |       49153 |                  |     2 | 2016-03-16 09:41:04.716
         20 |          1 |  3408 | svchost.exe             |       17 | fe80::1d77:665c:b2e5:3be3 |                           |       61180 |                  |     2 | 2016-03-16 09:41:04.716
         21 |          1 |  3408 | svchost.exe             |       17 | ::1                       |                           |       61181 |                  |     2 | 2016-03-16 09:41:04.716
         22 |          1 |   972 | svchost.exe             |        6 | 0.0.0.0                   | 0.0.0.0                   |         135 |                0 |     2 | 2016-03-16 09:41:04.716
         23 |          1 |     4 |                         |        6 | 192.168.0.22              | 0.0.0.0                   |         139 |                0 |     2 | 2016-03-16 09:41:04.716
         24 |          1 |  1728 | devmonsrv.exe           |        6 | 127.0.0.1                 | 0.0.0.0                   |         515 |                0 |     2 | 2016-03-16 09:41:04.716
         25 |          1 |  2208 | mdnsresponder.exe       |        6 | 127.0.0.1                 | 0.0.0.0                   |        5354 |                0 |     2 | 2016-03-16 09:41:04.716
         26 |          1 |  2564 | hostviewcli.exe         |        6 | 127.0.0.1                 | 0.0.0.0                   |       40123 |                0 |     2 | 2016-03-16 09:41:04.716
         27 |          1 |   716 | wininit.exe             |        6 | 0.0.0.0                   | 0.0.0.0                   |       49152 |                0 |     2 | 2016-03-16 09:41:04.716
         28 |          1 |   676 | svchost.exe             |        6 | 0.0.0.0                   | 0.0.0.0                   |       49153 |                0 |     2 | 2016-03-16 09:41:04.716
         29 |          1 |  1076 | svchost.exe             |        6 | 0.0.0.0                   | 0.0.0.0                   |       49154 |                0 |     2 | 2016-03-16 09:41:04.716
         30 |          1 |   772 | services.exe            |        6 | 0.0.0.0                   | 0.0.0.0                   |       49155 |                0 |     2 | 2016-03-16 09:41:04.716
         31 |          1 |  4872 | vpnui.exe               |        6 | 127.0.0.1                 | 127.0.0.1                 |       49156 |            62522 |     5 | 2016-03-16 09:41:04.716
         32 |          1 |   788 | lsass.exe               |        6 | 0.0.0.0                   | 0.0.0.0                   |       49157 |                0 |     2 | 2016-03-16 09:41:04.716
         33 |          1 |  3092 | uoipservice.exe         |        6 | 192.168.0.22              | 0.0.0.0                   |       49164 |                0 |     2 | 2016-03-16 09:41:04.716

0 个答案:

没有答案