Teradata 13:加入中的 CASE 语句
Teradata 13: CASE statement in Join
join中是否允许使用CASE语句?有什么好的方法可以完成这里的任务吗?我的真实查询还有其他一些左连接。
我想在这种情况下加入 T1 和 T2:
1.当T1.sub_service不为null时,加入T2.type
2.当T1.sub_service为null时,用T1.service与T2.type
连接
SELECT T1.service, T1.sub_service, T2.type
FROM TABLE1 T1
LEFT JOIN TABLE2 T2
ON T2.type LIKE
CASE WHEN T1.sub_service IS NULL THEN T1.service
WHEN T1.sub_service IS NOT NULL THEN T1.sub_service
END
只需将 LIKE 替换为 =:
ON T2.type =
CASE WHEN T1.sub_service IS NULL THEN T1.service
WHEN T1.sub_service IS NOT NULL THEN T1.sub_service
END
但您可以进一步将其简化为 COALESCE:
ON T2.type = COALESCE(T1.sub_service, T1.service)
join中是否允许使用CASE语句?有什么好的方法可以完成这里的任务吗?我的真实查询还有其他一些左连接。
我想在这种情况下加入 T1 和 T2: 1.当T1.sub_service不为null时,加入T2.type 2.当T1.sub_service为null时,用T1.service与T2.type
连接SELECT T1.service, T1.sub_service, T2.type
FROM TABLE1 T1
LEFT JOIN TABLE2 T2
ON T2.type LIKE
CASE WHEN T1.sub_service IS NULL THEN T1.service
WHEN T1.sub_service IS NOT NULL THEN T1.sub_service
END
只需将 LIKE 替换为 =:
ON T2.type =
CASE WHEN T1.sub_service IS NULL THEN T1.service
WHEN T1.sub_service IS NOT NULL THEN T1.sub_service
END
但您可以进一步将其简化为 COALESCE:
ON T2.type = COALESCE(T1.sub_service, T1.service)