Window function in MySQL queries

2019-04-12 01:50发布

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?

2条回答
Fickle 薄情
2楼-- · 2019-04-12 02:33

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

查看更多
家丑人穷心不美
3楼-- · 2019-04-12 02:41

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;
查看更多
登录 后发表回答