2

Stack Overflow 的新手(以及一般的编码)。

我做了一些研究,但无法找到以下问题的答案:

如何将两个表连接到应用于维度的函数结果,而不是维度本身?

即我想将以下两个表加入函数lower() 的小写结果,而不是加入大小写不明确的维度。

SELECT
lower(first_name) as firstname
,lower(last_name) as lastname
,lower(email) as email1
,total_donated
From BensData.Donations As a

JOIN EACH

(Select
lower(first_name) as first
,lower(last_name) as last
,lower(email) as email2
,sum(amount) as total_donated
From BensData.Donations 
GROUP BY email2, first, last) As b

ON a.email1=b.email2 AND a.firstname=b.first AND a.lastname=b.last

它不允许我加入我在第一个表 (a) 中创建的别名,但是,如果我加入表 a 中的原始维度(first_name 和 last_name),那么结果将基于大小写不明确的维度,并给出不希望的结果。

我希望这很清楚。

谢谢你的帮助!

4

3 回答 3

3

尝试使用两个这样的子查询:

SELECT
a.firstname
,a.lastname
,a.email1
,a.total_donated
FROM

(SELECT
lower(first_name) as firstname
,lower(last_name) as lastname
,lower(email) as email1
,total_donated
From BensData.Donations) As a

JOIN EACH

(Select
lower(first_name) as first
,lower(last_name) as last
,lower(email) as email2
,sum(amount) as total_donated
From BensData.Donations 
GROUP BY email2, first, last) As b

ON a.email1=b.email2 AND a.firstname=b.first AND a.lastname=b.last

在您的原始查询中,a 只是 BensData.Donations 的别名,因此您只能加入该表中存在的字段。

于 2014-12-18T19:35:21.257 回答
0

我从未听说过,join each也没有记录为 MySQL 连接的语法(请参阅此处)。

试试这个from子句:

From BensData.Donations a JOIN
     (Select lower(first_name) as first, lower(last_name) as last,
             sum(amount) as total_donated
      From BensData.Donations 
      GROUP BY first, last
     ) b
     ON a.firstname = b.first AND a.lastname = b.last

您的版本在列之间的子查询中也缺少逗号。

为清楚起见,您可能应该将from子句写为:

     ON lower(a.firstname) = b.first AND lower(a.lastname) = b.last

lower()从子查询中删除。否则,您将依赖于服务器、数据库和表的默认排序规则来执行该on子句的实际操作。

于 2014-12-18T02:53:17.407 回答
0

感谢大家的帮助!

特别是 sprocket,他为我指明了正确的方向!他的代码和我的主要区别在于我的代码没有在第一个 SELECT 子句的每个维度的前面附加表别名(例如 **a.**fistname, **a.**lastname, --- --> 名字,姓氏)

出于某种原因,BigQuery 由于表别名而不断给我一个错误。

这是有效的代码。

SELECT
firstname
,lastname
,email1
,total_donated
FROM

(SELECT
lower(first_name) as firstname
,lower(last_name) as lastname
,lower(email) as email1
From BensData.Donations) As a

JOIN EACH

(Select
lower(first_name) as first
,lower(last_name) as last
,lower(email) as email2
,sum(float(amount)) as total_donated
From BensData.Donations 
GROUP BY email2, first, last) As b

ON a.email1=b.email2 AND a.firstname=b.first AND a.lastname=b.last

感谢你的帮助!

于 2015-01-06T03:38:33.620 回答