【发布时间】:2019-12-19 11:41:45
【问题描述】:
我有以下两张表:
表1
shopid hour category amount
------------------------------------
1 7 food 10
1 8 food 15
1 10 misc. 5
...................................
表2
shopid hour category amount
------------------------------------
1 7 food 30
1 8 food 10
1 9 misc. 10
...................................
现在,我想根据它们的 shopid、event_hour 和类别合并这两个表。但是要计算金额上的加权和。条件是,如果我有相同的 shopid、小时、类别,那么它将执行加权和。否则,它将保留 table1 和 table2 中的原始数据。合并后的表格如下所示:
合并表
shopid hour category amount
------------------------------------
1 7 food 25 //amount= table1.amount*0.25+ table2.amount*0.75
1 8 food 11.25 //amount= table1.amount*0.25+ table2.amount*0.75
1 9 misc. 10 //this amount remains same as at hour 9 nothing was on table1.
1 10 misc. 5 //this amount remains same as at hour 10 nothing was on table2.
...................................
任何线索如何做到这一点?
【问题讨论】:
标签: sql postgresql union-all