如何在大查询中操作SAFE_ORDINAL

时间:2018-10-01 21:31:34

标签: google-bigquery

已更新。...再次尝试:

我正在使用此查询:

Select FirstName, LastName, MiddleName, Gender, Age, DOB, Address, Address2, City, State, Zip, Zip4, TimeZone, Income, HomeValue, Networth, MaritalStatus, IsRenter, HasChildren, CreditRating, Investor, LinesOfCredit, InvestorRealEstate, Traveler, Pets, MailResponder, Charitable, PolicalDonations, PoliticalParty, Attom_ID, GEOID, Score, Score1, Score2, Score3, Score4, Score5, Latitude, Longitude, Email[SAFE_ORDINAL(1)] Email1, Email[SAFE_ORDINAL(2)] Email2, Email[SAFE_ORDINAL(3)] Email3
from (

Select
  P.FirstName, P.LastName, MiddleName, Gender, Age, DOB, P.Address, Address2, P.City, P.State, P.Zip, Zip4, TimeZone, Income, HomeValue, Networth, MaritalStatus, IsRenter, HasChildren, CreditRating, Investor, LinesOfCredit, InvestorRealEstate, Traveler, Pets, MailResponder, Charitable, PolicalDonations, PoliticalParty, Attom_ID, GEOID, Score, Score1, Score2, Score3, Score4, Score5, Latitude, Longitude, E.Email
from `mother-216719.People.PEOPLE` P
join `mother-216719.People.EMAIL_STAGE` E on 
  P.FirstName = E.FirstName
  and P.LastName = E.LastName
  and P.Address = E.Address
  and P.Zip = E.Zip
 ) a

这是PEOPLE和EMAIL_STAGE之间的直接JOIN。 P.*且不带别名的所有FIELDS来自PEOPLE,而EMAIL来自EMAIL_STAGE作为字符串。

EMAIL_STAGE表具有名字,姓氏,地址,城市,州,邮政编码和EMAIL。仅那七个领域。 PEOPLE表具有其他字段,并且不需要其他几个字段。

我正在加入电子邮件,以通过地址匹配将电子邮件附加到PEOPLE。这里的关键是这种关系是一对多的,并且可能会有许多电子邮件发送给PEOPLE。我想将EMAIL1,EMAIL2,EMAIL3添加到PEOPLE中。我希望这些电子邮件来自PEOPLE的EMAIL_STAGE一对多关系。

我的第一个建议是使用NTH()执行类似PIVOT的操作,但是随后被告知SAFE_ORDINAL []是我想要的SQL Standard版本术语。上面的查询是结果,而查询中的结构错误是我要解决的问题。

如何从加入中PIVOT找到前三封电子邮件(不按顺序订购),并将其放入EMAIL1,EMAIL2,EMAIL3?

谢谢。

更新#2。

来自STAGE_EMAIL表的示例数据集:

名字|姓|地址|城市|州|邮政编码|电子邮件

Jael | Baird | 616 Lobortis Ave | Melipilla |圣地亚哥首都大都会|4513|vulputate.ullamcorper.magna@Crasvulputate.edu Yvette | Ellison | P.O。乌特路5270号847室|加斯提兹|尤斯卡迪|549851|Quisque.porttitor.eros@Duissit.org Lacota | Head | P.O。 Elit St. 7347 Box 161 | Coutisse | NA | E5R 7B5|metus.vitae@egestasa.com Victor | Hensley | 398-3949 Eget,St。|拉各斯|拉各斯| LI8 2ND|rhoncus.Proin@Phasellus.org

1 个答案:

答案 0 :(得分:2)

以下是用于BigQuery标准SQL

#standardSQL
SELECT FirstName, LastName, MiddleName, Gender, Age, DOB, Address, Address2, City, State, Zip, Zip4, TimeZone, Income, HomeValue, Networth, MaritalStatus, IsRenter, HasChildren, CreditRating, Investor, LinesOfCredit, InvestorRealEstate, Traveler, Pets, MailResponder, Charitable, PolicalDonations, PoliticalParty, Attom_ID, GEOID, Score, Score1, Score2, Score3, Score4, Score5, Latitude, Longitude, 
  Emails[SAFE_ORDINAL(1)] Email1, Emails[SAFE_ORDINAL(2)] Email2, Emails[SAFE_ORDINAL(3)] Email3
FROM (
  SELECT
    P.FirstName, P.LastName, MiddleName, Gender, Age, DOB, P.Address, Address2, P.City, P.State, P.Zip, Zip4, TimeZone, Income, HomeValue, Networth, MaritalStatus, IsRenter, HasChildren, CreditRating, Investor, LinesOfCredit, InvestorRealEstate, Traveler, Pets, MailResponder, Charitable, PolicalDonations, PoliticalParty, Attom_ID, GEOID, Score, Score1, Score2, Score3, Score4, Score5, Latitude, Longitude, E.Email
  FROM `mother-216719.People.PEOPLE` P
  JOIN (
    SELECT FirstName, LastName, Address, Zip, ARRAY_AGG(Email) Emails
    FROM `mother-216719.People.EMAIL_STAGE`
    GROUP BY FirstName, LastName, Address, Zip
  ) E 
  ON P.FirstName = E.FirstName
  AND P.LastName = E.LastName
  AND P.Address = E.Address
  AND P.Zip = E.Zip
) a

注意:未经测试,因此请告知是否仍然存在; 另外,E的子选择可能会调整为您需要从EMAIL_STAGE中选择的任何字段-我将根据需要留给您完成