【发布时间】:2017-04-25 12:49:59
【问题描述】:
我有一个主表,我的所有结果都将写入其中。 每个将被检查的对象都由 item_id 标识:
Checkdate item_id Price Cat A Price Cat B
2017-04-25 1 29.99 84.99
2017-04-24 1 39.99 89.99
2017-04-23 1 39.99 91.99
2017-04-25 2 42.99 88.99
2017-04-23 2 41.99 81.99
2017-04-22 2 50.99 81.99
2017-04-21 2 42.99 81.99
在 postgres 查询中,我选择 current_date = checkdate 的所有结果以提供最新数据:
Item Price Cat A Price Cat B
1 29.99 84.99
2 42.99 88.99
到目前为止,这对我来说不是问题。但现在我想将这些结果与以前的结果进行比较。类似的东西:
Item Price Cat A Price Cat A Before Price Cat B Price Cat B Before
1 29.99 39.99 84.99 89.99
2 42.99 41.99 88.99 81.99
但我不知道该怎么做。这些项目并非每天都存在(例如,项目 2 在 2017-04-24 不存在)。
有人可以帮我吗?
【问题讨论】:
-
条件
current_date = checkdate似乎暗示您每天检查所有(当前)项目。是这样吗?闻起来很有趣…… -
这有什么好笑的?该应用程序每天捕获一次最新数据并将其存储在数据库中。上面的查询将被 web 服务调用以获取最新数据(具有缓存功能)。此处需要 current_date,因为有时(如前所述)项目不存在(但明天可能会再次存在)。
标签: postgresql aggregate