【问题标题】:Consult the same table with filter value用过滤值查询同一张表
【发布时间】:2021-01-05 18:46:01
【问题描述】:

我会很感激我怎样才能得到这个结果。 我原来的表:

   Date     | Indicator |   Value
2020-01-01  |     1     |  3000.00
2020-01-02  |     1     |  2500.00
2020-01-03  |     1     |  1000.00
2020-01-01  |     2     |  12.50
2020-01-02  |     2     |  13.23
2020-01-03  |     2     |  14.24
2020-01-01  |     3     |  150.00
2020-01-02  |     3     |  300.00
2020-01-03  |     3     |  200.00

我需要为其余指标扩展指标 1 的值 尊重日期。

   Date     | Indicator |   Value   |  Result
2020-01-01  |     1     |  3000.00  |  3000.00
2020-01-02  |     1     |  2500.00  |  2500.00
2020-01-03  |     1     |  1000.00  |  1000.00
2020-01-01  |     2     |  12.50    |  3000.00
2020-01-02  |     2     |  13.23    |  2500.00
2020-01-03  |     2     |  14.24    |  1000.00
2020-01-01  |     3     |  150.00   |  3000.00
2020-01-02  |     3     |  300.00   |  2500.00
2020-01-03  |     3     |  200.00   |  1000.00

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。

标签: sql join select datatables


【解决方案1】:

一种方法使用窗口函数:

select t.*,
       first_value(value) over (partition by date order by indicator) as result
from t;

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 2016-09-25
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 2018-11-25
    • 2020-03-08
    相关资源
    最近更新 更多