分组PostgreSQL运行的总结果
我正试图得到2家商店的运行总数。我所做的查询将两个存储区的运行总数加在一起。如何更改查询以分别获取每个商店的运行总数?下面是我的问题。可以找到数据库 这是我得到的结果:分组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
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