Using Aliases in the ON Clause of a JOIN

Ben Leathers

New to Stack Overflow (and coding in general).

I did some research but was unable to find an answer to the following problem:

How can I join two tables ON the results of functions applied to dimensions, rather than on the dimensions themselves?

i.e. I want to join the following two tables on the lowercase results of the function lower() rather than joining on the case ambiguous dimensions as they are.

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

It does not let me join on the aliases I create in the first table (a), however, if I join ON the original dimensions in table a (first_name and last_name) then the results are based on the case ambiguous dimensions, and give an undesired result.

I hope that was clear.

Thanks for any help!

Ben Leathers

Thanks for everyone's help!

Particularly sprocket who pointed me in the right direction! The main difference in his code and mine is that mine does not have the table aliases appended on the front of each dimension of the first SELECT clause (e.g. **a.**fistname, **a.**lastname, -----> firstname, lastname)

For some reason BigQuery kept giving me an error because of the table aliases.

Here's the code that worked.

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

Thanks all for your help!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Aliases in the ON Clause of a JOIN

From Dev

SQL Join ON clause valid referencing on aliases

From Dev

SQL JOIN ON issue with aliases or not using aliases

From Dev

Using aliases in SQL inner join

From Dev

Using IN or JOIN or EXISTS CLAUSE

From Dev

using join and group by clause in mysql

From Dev

Using WHERE clause on NATURAL JOIN

From Dev

Using Trim in multiple where clause of linq with Join clause

From Dev

Searching using LEFT JOIN combined with WHERE clause

From Dev

Using Count aggregate clause within a join

From Dev

Selecting using SQL Subqueries or the JOIN Clause

From Dev

Using the WHERE clause in conjunction with NATURAL JOIN SQL?

From Dev

MySQLi join using date(now()) in where clause

From Dev

Using same 'WHERE' clause for JOIN and UNION and subqueries

From Dev

Multiple join clause using suqbqueries with multiple where

From Dev

sqlalchemy: select specific columns from multiple join using aliases

From Dev

Inner Join vs Natural Join vs USING clause: are there any advantages?

From Dev

SQL aliases on join

From Dev

what is the difference between using filter condition in WHERE clause and JOIN condition

From Dev

Django INNER JOIN using foreign key with WHERE clause

From Dev

Entity Framework + Linq LEFT JOIN using a where clause?

From Dev

Using a column in sql join without adding it to group by clause

From Dev

Having trouble adding a where clause to a join table using Sequelizer for NodeJS

From Dev

SQL Alternative for 'OR' in where clause when using outer join

From Dev

Query with JOIN clause always returns a empty result set using Dapper

From Dev

how to apply having, group by clause while using join for MySQL table

From Dev

Why does using the "AND" operator works with a JOIN, even without a "WHERE" clause

From Dev

Entity Framework + Linq LEFT JOIN using a where clause?

From Dev

Using a column in sql join without adding it to group by clause

Related Related

  1. 1

    Using Aliases in the ON Clause of a JOIN

  2. 2

    SQL Join ON clause valid referencing on aliases

  3. 3

    SQL JOIN ON issue with aliases or not using aliases

  4. 4

    Using aliases in SQL inner join

  5. 5

    Using IN or JOIN or EXISTS CLAUSE

  6. 6

    using join and group by clause in mysql

  7. 7

    Using WHERE clause on NATURAL JOIN

  8. 8

    Using Trim in multiple where clause of linq with Join clause

  9. 9

    Searching using LEFT JOIN combined with WHERE clause

  10. 10

    Using Count aggregate clause within a join

  11. 11

    Selecting using SQL Subqueries or the JOIN Clause

  12. 12

    Using the WHERE clause in conjunction with NATURAL JOIN SQL?

  13. 13

    MySQLi join using date(now()) in where clause

  14. 14

    Using same 'WHERE' clause for JOIN and UNION and subqueries

  15. 15

    Multiple join clause using suqbqueries with multiple where

  16. 16

    sqlalchemy: select specific columns from multiple join using aliases

  17. 17

    Inner Join vs Natural Join vs USING clause: are there any advantages?

  18. 18

    SQL aliases on join

  19. 19

    what is the difference between using filter condition in WHERE clause and JOIN condition

  20. 20

    Django INNER JOIN using foreign key with WHERE clause

  21. 21

    Entity Framework + Linq LEFT JOIN using a where clause?

  22. 22

    Using a column in sql join without adding it to group by clause

  23. 23

    Having trouble adding a where clause to a join table using Sequelizer for NodeJS

  24. 24

    SQL Alternative for 'OR' in where clause when using outer join

  25. 25

    Query with JOIN clause always returns a empty result set using Dapper

  26. 26

    how to apply having, group by clause while using join for MySQL table

  27. 27

    Why does using the "AND" operator works with a JOIN, even without a "WHERE" clause

  28. 28

    Entity Framework + Linq LEFT JOIN using a where clause?

  29. 29

    Using a column in sql join without adding it to group by clause

HotTag

Archive