【问题标题】:CrossData in PostgresPostgres 中的交叉数据
【发布时间】:2021-11-25 05:37:44
【问题描述】:

这是我第一次在这里提问。

我一直在使用Metabase,在我的工作中使用了一些关于 Postgres 的数据库。 我的需求是表之间的交叉数据,但主表没有日期列与其他表连接。 主要的方式,也就是模式是在一个日期阵营中加入de calendar_aux tablet。

名为 shop_dre_operational 的主表具有以下结构

year january february march april may june july august september october november december

返回值如下: Results

所有月份都是包含年份的列,我如何将日期转换为与其他表连接并选择一些值?

【问题讨论】:

    标签: sql database postgresql select business-intelligence


    【解决方案1】:

    您可以使用UNION ALL 以不同的格式组合数据。例如:

    select *
    from (
      select make_date(ano, 1, 1) as date, jan as amount from t
      union all select make_date(ano, 2, 1), fev from t
      union all select make_date(ano, 3, 1), mar from t
      ...
      union all select make_date(ano, 12, 1), dez from t
    ) x
    where ...
    group by ...
    order by ...
    

    将生成一个包含两列的数据集:dateamount,您可以在任何查询中使用它们。

    【讨论】:

    • 我已经实现了这个解决方案,将from t 替换为我在代码中使用的其他环境变量。成功了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多