簡體   English   中英

SQL Athena 查找函數

[英]SQL Athena lookup function

我的桌子是這樣的:

當前的日期 轉換1 轉換2 轉換3
2021-10-01 0.5 0.7 0.9
2021-10-02 0.4 0.6 0.8
2021-10-03 0.3 0.5 0.0
2021-10-04 0.2 0.0 0.0

而且我想根據日期將我的數據向下移動(如excel的查找功能)。

列轉換需要 1 天

列轉換縮短 2 天2

列轉換 3 天

所需的輸出應如下所示:

當前的日期 轉換1 轉換2 轉換3
2021-10-01 0.0 0.0 0.0
2021-10-02 0.5 0.0 0.0
2021-10-03 0.4 0.7 0.0
2021-10-04 0.3 0.6 0.9

謝謝你幫助我!

您是否嘗試過使用LAG

https://docs.aws.amazon.com/redshift/latest/dg/r_WF_LAG.html

像這樣的東西應該工作

選擇語句

SELECT date,
       coalesce(lag(conversion1) over (order by date), 0) conversion1,
       coalesce(lag(conversion2, 2) over (order by date), 0) conversion2,
       coalesce(lag(conversion3, 3) over (order by date), 0) conversion3
FROM TBL

架構構建

  tbl (
    Date date,
    conversion1 float,
    conversion2 float,
    conversion3 float
  );

INSERT INTO
  tbl
VALUES
  ('2020-10-01', 0.5, 0.7, 0.9),
  ('2020-10-02', 0.4, 0.6, 0.8),
  ('2020-10-03', 0.3, 0.5, 0),
  ('2020-10-04', 0.2, 0, 0)

SQL 小提琴(Postgres): http ://sqlfiddle.com/#!17/8bd32/4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM