如何基于两个表中的两个键列将sql查询写入union/join/或pivot数据到两个相同表中的列中?

xmd2e60i  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(400)

我有两个名为old\u user\u info和new\u user\u info的表。我需要在屏幕上显示的数据,要么加入或联合或他们的方式旋转,使他们在输出显示为一行,例如。
表用户信息\u旧:

UserID: 1
UserName: Jon Doe
Address: 2030 Pen St
Phone: 999-111-0990

表用户信息\u新建:

UserID: 1
UserName: Jon Doe
Address: 1090 Heming Ln
Phone: 809-290-8765

期望输出:

UserID  UserName  Old_Address  New_Address    Old_Phone    New_Phone

1       Jon Doe   2030 Pen St  1090 Heming Ln 999-111-0990 809-290-8765 
2       Jane Doe  909 Kit Ln   7677 Hawk st   876-990-3232 980-322-3333
..

以此类推,对于每行的其余记录,列中的新旧数据相邻。
我是新来使用透视功能,但我相信它将需要一些其他功能,如分区可能?
我尝试从一个表中选择列,然后将所有列与第二个表合并。我有一个外部选择,将选择最后一列,但我想我遗漏了一些东西。

SElect * from 
(
select a.userid, a.username, a.Address as Old_Address, a.phone as Old_Phone
from User_Info_Old a

Union All

SElect b.userid, b.username, b.Address as New_Address, b.phone as New_Phone
from User_Info_New b
)as sub
sort by UserID

期望输出:

UserID    UserName  Old_Address  New_Address    Old_Phone    New_Phone
1         Jon Doe   2030 Pen St  1090 Heming Ln 999-111-0990 809-290-8765
2         Jane Doe  909 Kit Ln   7677 Hawk st   876-990-3232 980-322-3333

但实际输出低于我想要的:

UserID         UserName       Old_Address       Old_Phone
1              Jon Doe        2030 Pen St       809-290-8765
Jon Doe        Jon Doe        999-111-0990      809-290-8765

以下是我正在使用的实际查询:

SELECT DISTINCT SEC.MeasureKey as Secondary_Measure_Key, SEC.Subkey as Secondary_Subkey, SEC.MeasureName as Secondary_Measure_Name, if(ISNull(PRI.Denom), Null,PRI.Denom) as Primary_Denom, 
if(ISNull(PRI.Num), Null,PRI.Num) as Primary_Num, if(ISNull(PRI.Rate), Null,PRI.Rate) as Primary_Rate,  if(ISNull(PRI.Contra), Null,PRI.Contra) as Primary_Contraindications, 
SEC.Denom as Secondary_DENOM, SEC.Num as Secondary_NUM, SEC.Rate as Secondary_Rate, 
SEC.Contra as Secondary_Contraindications, PRI.Population as Primary_Population, PRI.flowchart as Primary_flowchart, SEC.population as Secondary_Population, SEC.flowchart as Secondary_Flowchart, 
SEC.MeasureKey + '.' + SEC.Subkey as VLOOKUP 
From
(SELECT --returned 0 rows
DF3.flowchart_name as Flowchart,
DF3.population_name as Population,
Replace(DF3.measure_key,"19","") as MeasureKey, 
DF3.measure_short_name as MeasureName, 
DF3.submeasure_key as Subkey,
DF3.denominator as Denom,
DF3.numerator as Num,
DF3.contraindications as Contra,
DF3.rate as Rate

From DF3_MNR_PRD.Rate_Summary DF3
where DF3.date = '20190217') SEC

LEFT JOIN
(Select
DF2.flowchart_name as Flowchart,
DF2.population_name as Population,
Replace(DF2.measure_key,"18","") as MeasureKey, 
DF2.measure_short_name as MeasureName, 
DF2.submeasure_key as Subkey,
DF2.denominator as Denom,
DF2.numerator as Num,
DF2.contraindications as Contra,
DF2.rate as Rate

From DF2_MNR_PRD.Rate_Summary DF2
where DF2.date = '20180320') PRI

On SEC.MeasureKey = PRI.MeasureKey
and SEC.Subkey = PRI.Subkey

where SEC.Population = 'H4527_MCR-ALL_369'
and PRI.Population = 'H4527 MCR-ALL 369'
and SEC.Flowchart = 'PROD_MR_2019_2RUN1_FCR_02152019004120'
and PRI.Flowchart = 'PROD_MR_2018_MAR_3SDS2_FCR_03192018214442'

order by Secondary_Measure_Key, Secondary_Subkey
ncecgwcz

ncecgwcz1#

我想你只需要一个'左连接':

select 
    a.userid, a.username, 
    a.Address as Old_Address, b.Address as New_Address, 
    a.phone as Old_Phone, b.phone as New_Phone
from User_Info_Old a
left join User_Info_New b
on a.userid = b.userid
and a.username = b.username

如果遇到错误,请告诉我。

相关问题