Teradata,子查询以防万一
Teradata, subquery in case
你能帮帮我吗?我需要在 case 子查询中插入,但 teradata 不允许我执行此查询,我该如何更改它(与 case 类似的查询,如 if)?
select amount, status
case
when max(amount) then
(select sum(a1.amount)+a2.amount
from payments a1
join payments a2 on a1.payment_id=a2.payment_id
where a2.status = 't'
and a1.status not like 'w'
group by a1.amount,a2.amount)
else sum(amount)
end
from payments</pre>
Amount | Status
--------| ------
10 | t
20 | w
30 | t
40 | w
50 | t
60 | t
70 | k
预期结果:如果金额为最大值,则计算新列的金额,如 sum(a1.amount)+a2.amount,其中 table a1 的状态为 't' 和a2 的状态不是 'w'。如果金额不是最大值,则只需计算 table 付款的所有金额。
结果:
--对于 amount = 70 插入到新列(例如结果)sum(状态不是 'w' 的所有金额)+ sum(状态为 't' 的所有金额)。
--对于其他金额 < 70 插入新列结果总和(所有金额)。
感谢您的帮助。
我猜这会有所帮助
select amt,status,(case when amt=max_amt then w_sum else total_sum end)results
from (
select amt,
status,
sum(amt) over(partition by (case when status='w' and status<>'t' then 1 else 0 end ))w_sum,
sum(amt) over(partition by 1) total_sum,
max(amt) over(partition by 1) max_amt
from t st)x
让我知道它是否适合你。
你能帮帮我吗?我需要在 case 子查询中插入,但 teradata 不允许我执行此查询,我该如何更改它(与 case 类似的查询,如 if)?
select amount, status case when max(amount) then (select sum(a1.amount)+a2.amount from payments a1 join payments a2 on a1.payment_id=a2.payment_id where a2.status = 't' and a1.status not like 'w' group by a1.amount,a2.amount) else sum(amount) end from payments</pre>
Amount | Status --------| ------ 10 | t 20 | w 30 | t 40 | w 50 | t 60 | t 70 | k
预期结果:如果金额为最大值,则计算新列的金额,如 sum(a1.amount)+a2.amount,其中 table a1 的状态为 't' 和a2 的状态不是 'w'。如果金额不是最大值,则只需计算 table 付款的所有金额。
结果:
--对于 amount = 70 插入到新列(例如结果)sum(状态不是 'w' 的所有金额)+ sum(状态为 't' 的所有金额)。
--对于其他金额 < 70 插入新列结果总和(所有金额)。
感谢您的帮助。
我猜这会有所帮助
select amt,status,(case when amt=max_amt then w_sum else total_sum end)results
from (
select amt,
status,
sum(amt) over(partition by (case when status='w' and status<>'t' then 1 else 0 end ))w_sum,
sum(amt) over(partition by 1) total_sum,
max(amt) over(partition by 1) max_amt
from t st)x
让我知道它是否适合你。