在配置单元SQL中合并多个表
我有两个原始表:产品表和组件表 首先,我需要在这两个表之间进行内部合并,然后创建一个名为T1的新表:在配置单元SQL中合并多个表,sql,merge,hive,Sql,Merge,Hive,我有两个原始表:产品表和组件表 首先,我需要在这两个表之间进行内部合并,然后创建一个名为T1的新表: select a.Product, a.Plant, b.component, b.position, b.valid_date from Product a inner join component b on a.Product=b.Product where a.Plant='A' 结果T1如下所示: 此外,我需要基于T1创建一个名为T2的新表 select T1.Product,
select a.Product, a.Plant, b.component, b.position, b.valid_date from Product a
inner join component b on a.Product=b.Product
where a.Plant='A'
结果T1如下所示:
此外,我需要基于T1创建一个名为T2的新表
select T1.Product, T1.Plant,T1.position,max(T1.valid_date) as valid_date from T1
Where T1.Plant='A'
Group by T1.Product, T1.Plant,T1.position
结果T2是:
最后,我想根据最终表格的产品、工厂、位置和有效日期合并T1和T2:
select T2.Product, T2.Plant,T1.Component, T2.position, T2.valid_date from T1
INNER JOIN T2 on T1.Product=T2.Product and T1.Plant=T2.Plant and T1.position=T2.position and T1.valid_date=T2.valid_date
where T1.Plant='A'
最后一张表:
select T2.Product, T2.Plant,T1.Component, T2.position, T2.valid_date from T1
INNER JOIN T2 on T1.Product=T2.Product and T1.Plant=T2.Plant and T1.position=T2.position and T1.valid_date=T2.valid_date
where T1.Plant='A'
我知道整个过程可以在一个配置单元SQL脚本中完成。我对一个查询中的多个表感到困惑。我很感激有人能帮我。谢谢你不就是想这么做吗
select
a.Plant,b.Product,b.position,b.component,
max(valid_date) as valid_date
from Product a
inner join component b on a.Product = b.Product
where a.Plant = 'A'
group by a.Plant,b.Product,b.position,b.component
小提琴
上面的查询生成您在输出中显示的内容,但根据您的评论,我认为这就是您要查找的内容:
select t.Plant,t.Product,t.position,t.component, max(t.valid_date) valid_date
(
select
b.Product, a.Plant,b.component,b.position,max(valid_date) over (partition by a.Plant,b.Product,b.position) as valid_date
from Product a
join component b on a.Product = b.Product
where a.Plant = 'A'
) t
group by t.Plant,t.Product,t.position,t.component;
@钱如松,这个查询显示的是组件列,不确定得到T2的意思是基于组件之外的变量最大化日期。稍后,合并T1和T2将使组件返回以构建最终表。在一个查询中必须有两个选择。@QianruSong这正是您在输出中向我们显示的结果。在最终输出中,同一工厂和产品的每个组件的vbaliddate是不同的,是否为双倍最大值?valid_date未被识别为有效列。@QianruSong,嗯,所以它在配置单元中不起作用!请参阅更新的查询