Teradata 从多行中获取变量的值
Teradata Getting value of a variable from multiple rows
我有以下不同日期的日期和金额变量。根据最少的日期,我每个 ID 只需要一行。
我拥有的数据:
ID Date Code Amt
101 2/1/2018 A90 25
101 2/2/2018 B90 15
101 2/5/2018 A90 25
102 2/1/2018 B90 10
我要的数据:
ID A90_Date B90_Date A90_Amt B90_Amt
101 2/1/2018 2/2/2018 25 15
102 2/1/2018 10
我的查询:
Select ID,
min(case when Code='A90' then Date else Null end) as A90_Date,
min(case when Code='B90' then Date else Null end) as B90_Date,
min(case when Code='A90' then Amt else Null end) as A90_Amt,
min(case when Code='B90' then Amt else Null end) as B90_Amt
from Table
group by 1
虽然这给了我日期和金额值,但我担心它们是否准确来自超过 1MM 的记录。有没有一种有效的方法可以使用 Teradata
中的 Qualify
/Partition
语句提取这些数据?
提前感谢您的帮助。
如果相同 ID/code 的金额不同,您可能会得到错误的结果。那么你必须在聚合之前过滤最早的日期,例如
with cte as
(
Select ID, Code, Date, Amt
from Table
qualify row_number() over (partition by ID, Code order by date) = 1
)
Select ID,
min(case when Code='A90' then Date end) as A90_Date,
min(case when Code='B90' then Date end) as B90_Date,
min(case when Code='A90' then Amt end) as A90_Amt,
min(case when Code='B90' then Amt end) as B90_Amt
from cte
group by 1
根据您的数据,这可能更有效:
Select DISTINCT ID,
first_value(case when Code='A90' then Date end) over (partition by ID order by date) as A90_Date,
first_value(case when Code='B90' then Date end) over (partition by ID order by date) as B90_Date,
first_value(case when Code='A90' then Amt end) over (partition by ID order by date) as A90_Amt,
first_value(case when Code='B90' then Amt end) over (partition by ID order by date) as B90_Amt
from Table
group by 1
我有以下不同日期的日期和金额变量。根据最少的日期,我每个 ID 只需要一行。
我拥有的数据:
ID Date Code Amt
101 2/1/2018 A90 25
101 2/2/2018 B90 15
101 2/5/2018 A90 25
102 2/1/2018 B90 10
我要的数据:
ID A90_Date B90_Date A90_Amt B90_Amt
101 2/1/2018 2/2/2018 25 15
102 2/1/2018 10
我的查询:
Select ID,
min(case when Code='A90' then Date else Null end) as A90_Date,
min(case when Code='B90' then Date else Null end) as B90_Date,
min(case when Code='A90' then Amt else Null end) as A90_Amt,
min(case when Code='B90' then Amt else Null end) as B90_Amt
from Table
group by 1
虽然这给了我日期和金额值,但我担心它们是否准确来自超过 1MM 的记录。有没有一种有效的方法可以使用 Teradata
中的 Qualify
/Partition
语句提取这些数据?
提前感谢您的帮助。
如果相同 ID/code 的金额不同,您可能会得到错误的结果。那么你必须在聚合之前过滤最早的日期,例如
with cte as
(
Select ID, Code, Date, Amt
from Table
qualify row_number() over (partition by ID, Code order by date) = 1
)
Select ID,
min(case when Code='A90' then Date end) as A90_Date,
min(case when Code='B90' then Date end) as B90_Date,
min(case when Code='A90' then Amt end) as A90_Amt,
min(case when Code='B90' then Amt end) as B90_Amt
from cte
group by 1
根据您的数据,这可能更有效:
Select DISTINCT ID,
first_value(case when Code='A90' then Date end) over (partition by ID order by date) as A90_Date,
first_value(case when Code='B90' then Date end) over (partition by ID order by date) as B90_Date,
first_value(case when Code='A90' then Amt end) over (partition by ID order by date) as A90_Amt,
first_value(case when Code='B90' then Amt end) over (partition by ID order by date) as B90_Amt
from Table
group by 1