Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL unnest(数组_agg(x))_Sql_Postgresql_Sum - Fatal编程技术网

PostgreSQL unnest(数组_agg(x))

PostgreSQL unnest(数组_agg(x)),sql,postgresql,sum,Sql,Postgresql,Sum,假设我们有一张1号桌 1 2 3 a x 10 a y 20 b z 50 作为选择的结果,我们希望获得以下信息: 1 2 3 a x 30 a y 30 b z 50 至少是第1列相等的行的总和。我做了以下选择,它正在工作。但是select看起来很丑陋,有更聪明的解决方案吗 SELECT 1, UNNEST(ARRAY_AGG(2)), SUM(3) FROM table1 GROUP BY (1) 您可以改为进行窗口求和: select col1, col2, sum(col3) ov

假设我们有一张1号桌

1 2 3
a x 10
a y 20
b z 50
作为选择的结果,我们希望获得以下信息:

1 2 3
a x 30
a y 30
b z 50
至少是第1列相等的行的总和。我做了以下选择,它正在工作。但是select看起来很丑陋,有更聪明的解决方案吗

SELECT 1, UNNEST(ARRAY_AGG(2)), SUM(3) FROM table1
GROUP BY (1)

您可以改为进行窗口求和:

select col1, col2, sum(col3) over(partition by col1) col3
from mytable

您可以改为进行窗口求和:

select col1, col2, sum(col3) over(partition by col1) col3
from mytable