Window function in MySQL queries

2019-04-12 01:53发布

问题:

Is there a way to use window functions in MySQL queries dynamically within a SELECT query itself? (I know for a fact that it is possible in PostgreSQL).

For example, here is the equivalent query in PostgreSQL:

SELECT c_server_ip, c_client_ip, sum(a_num_bytes_sent) OVER 
   (PARTITION BY c_server_ip) FROM network_table;

However, what would be the corresponding query in MySQL?

回答1:

Hope this might work:

select A.c_server_ip, A.c_client_ip, B.mySum
 from network_table A, (
  select c_server_ip, sum(a_num_bytes_sent) as mySum
  from network_table group by c_server_ip
 ) as B
where A.c_server_ip=B.c_server_ip;


回答2:

Starting MySQL 8.0, you can now use OVER and partition, so consider upgrading to the latest version :)