【发布时间】:2021-09-28 06:10:23
【问题描述】:
假设我们有两个表执行左连接:
表 1
Joint Key || Attribute 1 || Attribute 2 || Attribute 3
A 1 11 21
B 2 12 22
C 3 13 23
表 2
Joint Key || Attribute 4 || Attribute 5
A 31 41
A 32 42
C 33 43
通过在“Joint Key”上执行table 1左连接table 2
它将返回两条记录
Joint Key = 'A'
Joint Key || Attribute 1 || Attribute 2 || Attribute 3 || Attribute 4 || Attribute 5
A 1 11 21 31 41
A 1 11 21 32 42
定义返回警察的最佳做法是什么,特别是在雪花中,可以返回与 表 1 相同的行数。
以上面的例子,我希望记录有MAX(Attribute 4)。我想到了两个初步的想法
选项1:使用“GROUP BY”子句——需要显式列出列,处理有很多列的表时很麻烦。
选项 2:类似
select * from (
select
Tabel1.*
max(Table2.Attribute_4) as mx_Attribute_4,
Table2.Attribute_5
from Table1
left join Table2
on Joint_Key
) as temp
where temp.Attribute_4 = temp.mx_Attribute_4
这也相当复杂和耗时。
还有其他建议吗?
【问题讨论】:
标签: sql tsql snowflake-cloud-data-platform