Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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/9.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运行的总结果_Sql_Postgresql - Fatal编程技术网

分组PostgreSQL运行的总结果

分组PostgreSQL运行的总结果,sql,postgresql,Sql,Postgresql,我正试图得到2家商店的运行总数。我所做的查询将两个存储区的运行总数加在一起。如何更改查询以分别获取每个商店的运行总数?下面是我的问题。可以找到数据库 这是我得到的结果: store id amont sum 1 5.99 5.99 1 0.99 6.98 1 9.99 16.97 1 4.99 21.96 2

我正试图得到2家商店的运行总数。我所做的查询将两个存储区的运行总数加在一起。如何更改查询以分别获取每个商店的运行总数?下面是我的问题。可以找到数据库

这是我得到的结果:

store id       amont      sum
1              5.99       5.99 
1              0.99       6.98 
1              9.99       16.97 
1              4.99       21.96 
2              2.99       24.95 
1              4.99       29.94 
1              0.99       30.93 
1              3.99       34.92 

在中按存储id使用分区

sum(amount) over (partition by store_id order by store_id, rental_date)

如果你想把它们放在不同的列中,你可以这样做

select
  store_id,
  amount,
  sum(case when store_id = 1 then amount else 0 end) over (order by rental_date) as store1_sum,
  sum(case when store_id = 2 then amount else 0 end) over (order by rental_date) as store2_sum,
from payment
join rental on payment.rental_id=rental.rental_id
join store  on payment.customer_id=store.store_id
对于不同的行,可以分区为@BarbarosÖzhan

select
  store_id,
  amount,
  sum(case when store_id = 1 then amount else 0 end) over (order by rental_date) as store1_sum,
  sum(case when store_id = 2 then amount else 0 end) over (order by rental_date) as store2_sum,
from payment
join rental on payment.rental_id=rental.rental_id
join store  on payment.customer_id=store.store_id