自上次提交记录以来金额是否有所增加?
Has amount increased since previous record submission?
我有一个table格式如下
Name ID Amount
Customer 1 1 £100
Customer 1 2 £50
Customer 1 3 £75
Customer 2 1 £0
Customer 2 2 £1000
Customer 3 1 £50
Customer 3 2 £20
Customer 3 3 £10
ID 将始终指示最新的字段,其中 1 = 最近的,如果找出客户看到哪些内容比他们以前的 ID 增加了价值,我需要做什么。
例如,客户 1 看到 1 增加了 50 英镑,客户 2 看到减少了 1000 英镑,客户 3 看到 2 增加了 10 英镑,另一个增加了 30 英镑。
感谢帮助,谢谢
select c1.*, c2.*
from customer c1
join customer c2
on c2.ID = c1.ID + 1
and c2.Name = c1.Name
and c2.Amnount < c1.Amnount
您需要一个移动差异,通常基于 LAG/LEAD。 Teradata 不支持此语法,但可以轻松重写:
Amount -
min(Amount) -- LEAD(Amount) over partition by Name order by ID)
over (partition by Name
order by ID
rows between 1 following and 1 following) as Diff
要仅过滤增加的内容,您可以添加
QUALIFY Diff > 0
我有一个table格式如下
Name ID Amount
Customer 1 1 £100
Customer 1 2 £50
Customer 1 3 £75
Customer 2 1 £0
Customer 2 2 £1000
Customer 3 1 £50
Customer 3 2 £20
Customer 3 3 £10
ID 将始终指示最新的字段,其中 1 = 最近的,如果找出客户看到哪些内容比他们以前的 ID 增加了价值,我需要做什么。
例如,客户 1 看到 1 增加了 50 英镑,客户 2 看到减少了 1000 英镑,客户 3 看到 2 增加了 10 英镑,另一个增加了 30 英镑。
感谢帮助,谢谢
select c1.*, c2.*
from customer c1
join customer c2
on c2.ID = c1.ID + 1
and c2.Name = c1.Name
and c2.Amnount < c1.Amnount
您需要一个移动差异,通常基于 LAG/LEAD。 Teradata 不支持此语法,但可以轻松重写:
Amount -
min(Amount) -- LEAD(Amount) over partition by Name order by ID)
over (partition by Name
order by ID
rows between 1 following and 1 following) as Diff
要仅过滤增加的内容,您可以添加
QUALIFY Diff > 0