mysql - SQL percentage aggregation over group by clause -


i have table gives following sql query inputs gives.

select server, count(*) servernames server not null , server != '' , timestamp >= '2015-03-18' , timestamp <= '2015-04-19' group server;  server | count(*) _________________ server1    1700   server2    1554  select server, ip_address, count(*) servernames server not null , server != '' , ip_address not null , ip_address != '' , timestamp >= '2015-03-18' , timestamp <= '2015-04-19'  group server, ip_address;  server |    ip_address      | count(*) ______________________________________ server1  sample_ip_1            14 server2  sample_ip_2           209 server1  sample_ip_2           100 server1  sample_ip_1            50 

i finding difficulty in writing query calculates percentage within group . instance in example output should be.

server | ip_address | count(*) | percent ________________________________________________ server1  sample_ip_1  14         0.82% (14/1700) server2  sample_ip_2 209         13.44%(209/1554) server1  sample_ip_2 100         5.88%(100/1700) server2  sample_ip_1  50         3.217(15/1554) 

how can write query that?

you need join results 2 queries together, , concat bunch of stuff percent value you're looking for. think should it.

select q2.server, q2.ip_address, concat(round((q2.c / q1.c) * 100, 2), '%(', q2.c, '/', q1.c, ')') percent        (       select server, count(*) c          servernames          server not null            , server != ''            , timestamp >= '2015-03-18'           , timestamp <= '2015-04-19'          group server      ) q1      inner join      (        select server, ip_address, count(*) c            servernames           server not null             , server != ''             , ip_address not null             , ip_address != ''             , timestamp >= '2015-03-18'            , timestamp <= '2015-04-19'            group server, ip_address      ) q2      on q1.server = q2.server 

demo here


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -