改进 SQL teradata 查询
Improving a SQL teradata query
我有一个如下所示的 table,我希望在结果集中的 Ref 345 和 789 前面 'Y' 基于 count(Ref) = 1,其中金额小于0. 我正在使用此查询来获得所需的输出。我的问题是,在 Teradata 中还有其他(更有效的)方法吗?
SELECT T.Ref,T.AMOUNT, R.Refund_IND as Refund_IND
FROM Table1 t
LEFT JOIN (select 'Y' as Refund_IND, Ref from Table1 where Ref in
(select Ref from Table1 where amount < 0)
group by Ref having count(Ref) = 1) R on t.Ref = R.Ref
您可以使用 window 函数来测试这些条件:
SELECT
Ref,
Amount,
CASE WHEN COUNT(*) OVER (PARTITION BY REF) = 1 AND Amount < 0 THEN 'Y' ELSE '' END AS Refund_Ind
FROM Table1
我有一个如下所示的 table,我希望在结果集中的 Ref 345 和 789 前面 'Y' 基于 count(Ref) = 1,其中金额小于0. 我正在使用此查询来获得所需的输出。我的问题是,在 Teradata 中还有其他(更有效的)方法吗?
SELECT T.Ref,T.AMOUNT, R.Refund_IND as Refund_IND
FROM Table1 t
LEFT JOIN (select 'Y' as Refund_IND, Ref from Table1 where Ref in
(select Ref from Table1 where amount < 0)
group by Ref having count(Ref) = 1) R on t.Ref = R.Ref
您可以使用 window 函数来测试这些条件:
SELECT
Ref,
Amount,
CASE WHEN COUNT(*) OVER (PARTITION BY REF) = 1 AND Amount < 0 THEN 'Y' ELSE '' END AS Refund_Ind
FROM Table1