使用GROUP_CONCAT返回一行

时间:2017-12-26 15:41:20

标签: mysql sql

我的查询有问题,我想将所有会计数据组合在一起,而GROUP_CONCAT则是客户站点名称和站点IP。

我使用了GROUP_CONCAT(site_name) as site_nameGROUP_CONCAT(site_ip) as site_ip,但我得到的结果仍然不正确,我在GROUP_CONCAT列中获得了重复的IP。

我需要为每个客户端提供一行数据,并为每个属于他们的站点IP汇总SUM / Grouped。

我的查询是:

SELECT 
SUM(upload_bytes) as upload_bytes, 
SUM(download_bytes) as download_bytes, 
SUM(upload_bytes + download_bytes) as totalbytes, 
package_id, 
username, 
userid, 
networkaccess, 
packagename, 
speedlimit, 
threshold, 
throttlelimit, 
extendeddata, 
datalimitamount, 
accountingdays, 
GROUP_CONCAT(site_name) as site_name, 
GROUP_CONCAT(site_ip) as site_ip 
FROM 
(
    (
        SELECT 
            ip_accounting.src_address as site_ip, 
            SUM(ip_accounting.bytes) AS upload_bytes, 
            0 as download_bytes, 
            clients.username, 
            clients.userid, 
            clients.networkaccess, 
            clients.extendeddata, 
            data_packages.package_id, 
            data_packages.packagename, 
            data_packages.speedlimit, 
            data_packages.threshold, 
            data_packages.throttlelimit, 
            data_packages.datalimitamount, 
            data_packages.accountingdays, 
            client_site_ip.site_name 
        FROM 
            ip_accounting 
            join client_site_ip on client_site_ip.site_ip = ip_accounting.src_address 
            JOIN clients ON client_site_ip.userid = clients.userid 
            join data_packages on data_packages.package_id = clients.datapackage 
        WHERE 
            dst_address NOT BETWEEN INET_NTOA('192.168.0.1') 
            AND INET_NTOA('192.168.255.254') 
            and timeanddate BETWEEN SUBDATE(
                CURRENT_TIMESTAMP(), 
                INTERVAL data_packages.accountingdays DAY
            ) 
            AND CURRENT_TIMESTAMP() 
        GROUP BY 
            src_address
    ) 
    UNION ALL 
        (
            SELECT 
                ip_accounting.dst_address as site_ip, 
                0 AS upload_bytes, 
                SUM(ip_accounting.bytes) as download_bytes, 
                clients.username, 
                clients.userid, 
                clients.networkaccess, 
                clients.extendeddata, 
                data_packages.package_id, 
                data_packages.packagename, 
                data_packages.speedlimit, 
                data_packages.threshold, 
                data_packages.throttlelimit, 
                data_packages.datalimitamount, 
                data_packages.accountingdays, 
                client_site_ip.site_name 
            FROM 
                ip_accounting 
                join client_site_ip on client_site_ip.site_ip = ip_accounting.dst_address 
                JOIN clients ON client_site_ip.userid = clients.userid 
                join data_packages on data_packages.package_id = clients.datapackage 
            WHERE 
                src_address NOT BETWEEN INET_NTOA('192.168.0.1') 
                AND INET_NTOA('192.168.255.254') 
                and timeanddate BETWEEN SUBDATE(
                    CURRENT_TIMESTAMP(), 
                    INTERVAL data_packages.accountingdays DAY
                ) 
                AND CURRENT_TIMESTAMP() 
            GROUP BY 
                dst_address
        )
) a 
GROUP BY 
site_ip 
ORDER BY 
INET_ATON(site_ip)

我目前的结果如下:

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

您应该按非聚合列进行分组(而不是您在聚合函数中使用的列),例如:

  SELECT 
  SUM(upload_bytes) as upload_bytes, 
  SUM(download_bytes) as download_bytes, 
  SUM(upload_bytes + download_bytes) as totalbytes, 
  package_id, 
  username, 
  userid, 
  networkaccess, 
  packagename, 
  speedlimit, 
  threshold, 
  throttlelimit, 
  extendeddata, 
  datalimitamount, 
  accountingdays, 
  GROUP_CONCAT(DISTINCT site_name) as site_name, 
  GROUP_CONCAT(DISTINCT site_ip) as site_ip 
  FROM 
  (
      (
          SELECT 
              ip_accounting.src_address as site_ip, 
              SUM(ip_accounting.bytes) AS upload_bytes, 
              0 as download_bytes, 
              clients.username, 
              clients.userid, 
              clients.networkaccess, 
              clients.extendeddata, 
              data_packages.package_id, 
              data_packages.packagename, 
              data_packages.speedlimit, 
              data_packages.threshold, 
              data_packages.throttlelimit, 
              data_packages.datalimitamount, 
              data_packages.accountingdays, 
              client_site_ip.site_name 
          FROM 
              ip_accounting 
              join client_site_ip on client_site_ip.site_ip = ip_accounting.src_address 
              JOIN clients ON client_site_ip.userid = clients.userid 
              join data_packages on data_packages.package_id = clients.datapackage 
          WHERE 
              dst_address NOT BETWEEN INET_NTOA('192.168.0.1') 
              AND INET_NTOA('192.168.255.254') 
              and timeanddate BETWEEN SUBDATE(
                  CURRENT_TIMESTAMP(), 
                  INTERVAL data_packages.accountingdays DAY
              ) 
              AND CURRENT_TIMESTAMP() 
          GROUP BY 
              src_address
      ) 
      UNION ALL 
          (
              SELECT 
                  ip_accounting.dst_address as site_ip, 
                  0 AS upload_bytes, 
                  SUM(ip_accounting.bytes) as download_bytes, 
                  clients.username, 
                  clients.userid, 
                  clients.networkaccess, 
                  clients.extendeddata, 
                  data_packages.package_id, 
                  data_packages.packagename, 
                  data_packages.speedlimit, 
                  data_packages.threshold, 
                  data_packages.throttlelimit, 
                  data_packages.datalimitamount, 
                  data_packages.accountingdays, 
                  client_site_ip.site_name 
              FROM 
                  ip_accounting 
                  join client_site_ip on client_site_ip.site_ip = ip_accounting.dst_address 
                  JOIN clients ON client_site_ip.userid = clients.userid 
                  join data_packages on data_packages.package_id = clients.datapackage 
              WHERE 
                  src_address NOT BETWEEN INET_NTOA('192.168.0.1') 
                  AND INET_NTOA('192.168.255.254') 
                  and timeanddate BETWEEN SUBDATE(
                      CURRENT_TIMESTAMP(), 
                      INTERVAL data_packages.accountingdays DAY
                  ) 
                  AND CURRENT_TIMESTAMP() 
              GROUP BY 
                  dst_address
          )
  ) a 
  GROUP BY 
      package_id, 
      username, 
      userid, 
      networkaccess, 
      packagename, 
      speedlimit, 
      threshold, 
      throttlelimit, 
      extendeddata, 
      datalimitamount, 
      accountingdays