Sql 如何连接上个月缺少行的两个表?

Sql 如何连接上个月缺少行的两个表?,sql,r,postgresql,window-functions,Sql,R,Postgresql,Window Functions,我想联接两个表,其中一个表缺少每个货币组的一行 表: 按月份汇总的本地货币交易记录。交易记录表 Date Currency spend 2019-01-01 EUR 100 2019-02-01 EUR 200 2019-03-01 EUR 500 2019-04-01 EUR 214 2019-01-01

我想联接两个表,其中一个表缺少每个货币组的一行

表:

按月份汇总的本地货币交易记录。交易记录表

Date            Currency       spend
2019-01-01       EUR             100
2019-02-01       EUR             200 
2019-03-01       EUR             500
2019-04-01       EUR             214
2019-01-01       JYP            3200
2019-01-01       JYP            1534
2019-02-01       JYP            1534
2019-03-01       JYP            1534
2019-04-01       JYP            1534
每月汇率变动\数据表

Month            Currency       Average Monthly rate
2019-01-01       EUR            1.2
2019-02-01       EUR            1.3 
2019-03-01       EUR            1.4
2019-01-01       JYP            101
2019-02-01       JYP            102
2019-03-01       JYP            103
2019-01-01       USA            1
2019-02-01       USA            1
2019-03-01       USA            1
我想执行一个联接以获得所有美元交易。问题是当前月份2019-04-01的费率不可用。因此,在联接之后,当前月份的所有事务都返回NULL

我已经设法在R中解决了它,但是有没有办法用SQL解决它呢? 我一直在尝试使用窗口功能,但没有成功

LAG(rate,1) OVER (PARTITION BY currency ORDER BY month)
R中的解:假设速率保持不变

library(lubridate)
library(dplyr)

exchange_previous <- exchange_data[exchange_data$month == floor_date(today(),"month") %m-% months(1),]
exchange_previous$month <- exchange_previous$month %m+% months(1)
exchange_data<-rbind(exchange_data,exchange_previous)

final <- transactions %>%
left_join(exchange_data, by = c("currency" = "name", "floor_date" = "month"))
Then simply multiply

假设您的表被称为Transactions和Exchange,下面的代码适用于MySQL。我想Postgresql也会类似

select C.date, C.spend/E.Average 
   from Transactions as C, Exchange as E,
      (select A.date, A.Currency, Max(B.Month) as Month 
         from Transactions as A, Exchange as B
         where A.Currency = B.Currency and A.datum >= B.Month
         group by A.date, A.Currency
      ) as D
where C.date = D.date 
   and C.Currency = D.Currency 
   and E.Month = D.Month 
   and C.Currency = E.Currency
order by C.date;

其背后的想法如下:给定日期和货币,请查看表D,该表是该货币最近的过去日期。一旦你有了这些,你就可以得到你想要的交换了。

假设你的表叫做事务和交换,下面的代码适用于MySQL。我想Postgresql也会类似

select C.date, C.spend/E.Average 
   from Transactions as C, Exchange as E,
      (select A.date, A.Currency, Max(B.Month) as Month 
         from Transactions as A, Exchange as B
         where A.Currency = B.Currency and A.datum >= B.Month
         group by A.date, A.Currency
      ) as D
where C.date = D.date 
   and C.Currency = D.Currency 
   and E.Month = D.Month 
   and C.Currency = E.Currency
order by C.date;
其背后的想法如下:给定日期和货币,请查看表D,该表是该货币最近的过去日期。一旦你有了它,你就可以得到你想要的外汇。

你可以使用横向联接来应用一种货币的最新汇率,如下所示:

选择tr.*,rates.rate 来自交易tr 左连接 横向选择率 -按日期排序行,最近的第一行 ,按货币划分的行数 按d描述订购 从交换数据 其中currency=tr.currency -交换日期仅限于交易当月 和d您可以使用横向联接来应用货币的最新汇率,如下所示:

选择tr.*,rates.rate 来自交易tr 左连接 横向选择率 -按日期排序行,最近的第一行 ,按货币划分的行数 按d描述订购 从交换数据 其中currency=tr.currency -交换日期仅限于交易当月
d使用横向连接,但应如下所示:

select t.*, ed.average_monthly_rate,
from transactions t left join lateral
     (select ed.*
      from exchange_data ed
      where ed.currency = t.currency and
            ed.month <= t.date
      order by ed.month desc
      fetch first 1 row only
     ) ed
     on 1=1;

我不确定是否要除以或乘以速率。

使用横向连接,但它应该如下所示:

select t.*, ed.average_monthly_rate,
from transactions t left join lateral
     (select ed.*
      from exchange_data ed
      where ed.currency = t.currency and
            ed.month <= t.date
      order by ed.month desc
      fetch first 1 row only
     ) ed
     on 1=1;

我不确定您是否要除以或乘以速率。

@GordonLinoff postgresql请为那些不使用R的Postgres用户发布所需的结果或R代码的输出。所有的JPP都是JYP吗?如果没有汇率,你会用上个月的汇率吗?@xavier是的,我的错。JPP是JYP。是的,上一个month@GordonLinoffPostgreSql请为那些不使用R的Postgres用户发布所需的结果或R代码输出。所有JPP都是JYP吗?如果没有汇率,你会用上个月的汇率吗?@xavier是的,我的错。JPP是JYP。是的,在上一个monthWell玩的游戏中,我没有想到横向连接中的逐月顺序描述限制1。玩得好,我没有想到横向连接中的逐月顺序描述限制1。Gordon的答案使用逐月顺序描述限制1更好。Gordon的答案使用逐月顺序描述限制1更好。