【发布时间】:2019-01-06 18:28:41
【问题描述】:
我正在使用以下方法连接两个表:
select table1.date, table1.item, table1.qty, table2.anotherQty
from table1
INNER JOIN table2
on table1.date = table2.date
table1
date | item | qty
july1 | itemA | 20
july1 | itemB | 30
july2 | itemA | 20
table2
date | anotherQty
july1 | 200
july2 | 300
预期结果应该是:
date | item | qty | anotherQty
july1 | itemA | 20 | 200
july1 | itemB | 30 | null or 0
july2 | itemA | 20 | 300
这样当我 sum(anotherQty) 时,它将只有 500,而不是:
date | item | qty | anotherQty
july1 | itemA | 20 | 200
july1 | itemB | 30 | 200
july2 | itemA | 20 | 300
即 200+200+300 = 700
【问题讨论】:
-
真正最好的办法是在加入之前在原始表中对
anotherQty求和。 -
两个表之间唯一的关联是日期列。那么,为什么您会期望“200”出现在带有“itemA”的行上呢?为什么不是“itemB”或任何其他“july1”记录?
标签: postgresql duplicates jointable