简体   繁体   English

在单行 Netezza/Postgres 中加入多行到多列

[英]JOIN multiple rows to multiple columns in single row Netezza/Postgres

I have a table A that looks like this我有一个看起来像这样的表 A

Date         Name      Value
----------------------------
2015-01-01   A         12
2015-01-01   B         13
2015-01-01   C         10
2015-01-01   D          9
2015-01-01   E         15
2015-01-01   F         11
2015-01-02   A          1
2015-01-02   B          2
2015-01-02   C          3
2015-01-02   D          4
2015-01-02   E          5
2015-01-02   F          6
2015-01-03   A          7
2015-01-03   B          8
2015-01-03   C          9
2015-01-03   D         10
2015-01-03   E         15
2015-01-03   F         16
....

Which contains a value for each name for each day.其中包含每天每个名称的值。 I need a second table which looks like this我需要一个看起来像这样的第二张桌子

Date         Name    ValueDate    ValueDate+1     ValueDate+2
--------------------------------------------------------------
2015-01-01   A         12            1                7
2015-01-01   B         13            2                8
2015-01-01   C         10            3                9
2015-01-01   D          9            4               10
2015-01-01   E         15            5               15
2015-01-01   F         11            6               16
2015-01-02   A          1            7              ...
2015-01-02   B          2            8              ...
2015-01-02   C          3            9              ...
2015-01-02   D          4           10              ...
2015-01-02   E          5           15              ...
2015-01-02   F          6           16              ...

I tried creating an intermediate table which has all the dates correctly entered我尝试创建一个中间表,其中正确输入了所有日期

Date         Name    ValueDate      ValueDate+1     ValueDate+2
----------------------------------------------------------------
2015-01-01   A         2015-01-01    2015-01-02      2015-01-03
2015-01-01   B         2015-01-01    2015-01-02      2015-01-03
2015-01-01   C         2015-01-01    2015-01-02      2015-01-03
2015-01-01   D         2015-01-01    2015-01-02      2015-01-03
2015-01-01   E         2015-01-01    2015-01-02      2015-01-03
2015-01-01   F         2015-01-01    2015-01-02      2015-01-03
...

My idea then was to use some kind of JOIN on table a to map the the corresponded Values to the dates and use s.th like我的想法是在表 a 上使用某种 JOIN 将对应的值映射到日期并使用 s.th 之类的

CASE WHEN Date = ValueDate THEN Value ELSE NULL END AS ValueDate+1

I am struggling to figure out how this can be done in SQL.我正在努力弄清楚如何在 SQL 中做到这一点。 I essentially need all the Values over a window for an initial date sequence.对于初始日期序列,我基本上需要窗口上的所有值。 To give some background I want to see for a regular time interval how the value behaves in the following x days.为了提供一些背景知识,我想在固定的时间间隔内查看该值在接下来的 x 天中的行为。 The Datatypes are Date for all the Date columns, Varchar for the Name and numerics for the Values.数据类型是所有日期列的日期,名称的 Varchar 和值的数字。 The ValueDate+1 and +2 means +1/2 days. ValueDate+1 和 +2 表示 +1/2 天。 Also it cannot be ruled out that the counts of names stays constant over time.也不能排除名称的计数随着时间的推移保持不变。

thanks谢谢

You just want lead() :你只想要lead()

select a.*,
       lead(value) over (partition by name order by date) as value_1,
       lead(value, 2) over (partition by name order by date) as value_2
from a;

I'm unclear about your problem statement --我不清楚你的问题陈述——

First, you don't say if the "Date" in your data is a Date type, or just a string that looks like a date.首先,您不会说数据中的“日期”是日期类型,还是只是一个看起来像日期的字符串。

Is "Value" the primary key for each line of data? “值”是每行数据的主键吗? Or is it an actual value used in the computation of "ValueDate+1"?或者它是用于计算“ValueDate+1”的实际值? If so, how?如果是这样,如何?

Is "ValueDate" simply the value of "Date", and "ValueDate+1" the day after the "Date"? “ValueDate”是否只是“Date”的值,而“ValueDate+1”是“Date”之后的第二天? Or is it the month after?还是后一个月?

Is ValueDate supposed to be the value of the "Date" plus "Value" days? ValueDate 是否应该是“Date”加上“Value”天的值?

Could you restate the problem, and include types of your data?你能重述这个问题,并包括你的数据类型吗?

Can you state the problem with equations, or formulas, along with the type of output values you need to show?你能用方程或公式说明问题,以及你需要显示的输出值的类型吗?

If you can, I can show you how to format it in SQL.如果可以,我可以向您展示如何在 SQL 中对其进行格式化。

-- Ada ——艾达

I found one way of getting the desired results, by writing a row_number() sub select limit to the desired window size.我找到了一种获得所需结果的方法,方法是将 row_number() 子选择限制写入所需的窗口大小。 Which gives each entry per date s.th like this这给每个日期的每个条目 s.th 像这样

Date         Name      Value    Row_Num
---------------------------------------
2015-01-01    A         12        0
2015-01-01    A         12        1
2015-01-01    A         12        2
2015-01-01    A         12        3

In the next step one can use在下一步中可以使用

(Date + Row_Num*INTERVAL'1 DAY')::DATE 

which then can be joined on the initial table and pivoted.然后可以在初始表上加入并旋转。 This will allow for any arbitrary combination of Names per date.这将允许每个日期的名称的任意组合。

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

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