【问题标题】:Rolling join for pandas , python or oraclepandas、python 或 oracle 的滚动连接
【发布时间】:2017-07-21 17:06:47
【问题描述】:

我对 Oracle 或 Python pandas 中的滚动联接有疑问。

我有一张标价表、年份表、材料表

我有另一个类似于明细表的表,其中包含报价、年份、材料、公司等...

我想在第二个表中获取第一个表中最新的材料的标价。

基本上,我想比较每种材料的报价和该日期或之前最接近的给定标价。

有人可以帮忙吗,我听说过滚动连接,但不确定如何在 oracle 或 Pandas 中执行此操作

【问题讨论】:

标签: python oracle pandas join


【解决方案1】:

您要求的结果不明确(您希望从哪个列表中获得最新信息?),但这里有几个想法:

在 Oracle 中执行此操作的愚蠢方法:从一个或另一个表中提取最新日期,然后进行连接:

select material, company, ..., quote_price, quote_date,
       table1_latest.max_list_date, table1_latest.list_price 
from detail_table

left outer join

(select max(date) max_list_date, list_price, material from table1
 group by list_price, material) table1_latest

 ON detail_table.material = table1_latest.material

编辑:做一个“完整”连接,然后找出日期之间的差异,使用最小的差异

select min(abs(days_diff)) min_days_diff, material, list_price,
       list_date, quote_date as closest_quote_date, quote_price as closest_quote_price
       from
       (select list_table.material, list_table.list_price,
               list_table.list_date, quote_table.quote_price,
               quote_table.quote_date, 
               (quote_table.quote_date-list_table.list_date) days_diff
        from list_table
        left outer join
        quote_table
        on list_table.material = quote_table.material) joined_table
group by material, list_price, list_date, quote_date, quote_price

Left Outer Join 获取每个标价的所有可用报价。只要这两个日期是Oracle Date 类型,就可以做减法;它会产生天数差异。

【讨论】:

  • 不,我想要在明细表中关闭到特定日期的数据。
  • 就像我的明细表在 7 月 21 日有某个产品的价格,所以我想看看,其他表的价格是多少或最接近该数据并将其放在我的表旁边
猜你喜欢
  • 1970-01-01
  • 2015-11-25
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多