CrossData in Postgres



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

我一直在工作与Metabase与Postgres上的一些数据库。我有一个跨表数据的需求,但是主表没有Date列与其他列连接。主要的方式,也就是模式是在Date camp中使用de calendar_aux tablet进行JOIN。

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

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

返回如下值:结果

所有的月份都是列,包括年,我如何在日期中转换它以与其他表连接并选择一些值?

您可以使用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,您可以在任何查询中使用。

最新更新