繁体   English   中英

如何在上个月联接两个缺少行的表?

[英]How to joining two tables with missing rows in the last month?

我想加入两个表,每个货币组都缺少一行。

表格:

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

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

月汇率(exchange_data表)

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

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

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;

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

您可以使用LATERAL JOIN来应用一种货币的最新汇率,如下所示:

        SELECT tr.*, rates.rate
          FROM transactions tr
          LEFT JOIN
       LATERAL (SELECT rate
                     -- order rows by date, most recent first
                     , ROW_NUMBER() OVER (PARTITION BY currency 
                                              ORDER BY d DESC) rn 
                  FROM exchange_data 
                 WHERE currency = tr.currency 
                   -- exchange dates only up until the month of a transaction
                   AND d <= tr.d
             ) rates
            ON rates.rn = 1 -- row for the most recent date

LATERAL JOIN与SQL Server中的APPLY相似,因为它使您可以使用外部查询中的谓词联接重新集。

这是dbfiddle上的工作示例

假设您的表称为事务和交换,则以下代码适用于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,该表D是该货币最近的日期。 有了这些,就可以得到您想要的交换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM