SQL Syntax JOIN google bigquery - google-bigquery

I am getting this error in Google BigQuery:
Error: Ambiguous field reference in SELECT clause. (Remember to fully qualify all field names in the SELECT clause as <table.field>.)
My query is
SELECT LoanPerf1.loankey, Loans.Key
FROM prosperloans1.LoanPerf1
JOIN prosperloans1.Loans
ON LoanPerf1.loankey = Loans.Key
prosperloans1 is the dataset id
the 2 table names are correct.
the error returns regardless of which field name appears first in the select clause.
The documentation on Google SQL Syntax says this is correct:
// Simple JOIN of two tables
SELECT table1.id, table2.username
FROM table1
JOIN table2
ON table1.name = table2.name AND table1.id = table2.customer_id;
Thanks
Shawn

Try adding an AS clause to the table names:
SELECT LoanPerf1.loankey, Loans.Key
FROM prosperloans1.LoanPerf1 as LoanPerf1
JOIN prosperloans1.Loans as Loans
ON LoanPerf1.loankey = Loans.Key

Related

Outer join for Alias name and column name -Oracle

I had a working sample query earlier in my code as mentioned below.
SELECT DISTINCT
nombre_aplicacion,
APLICACION,
NOMBRE_APLCODE,
DESCRIPCION,
AREAFUNC
FROM (
select **CODAPLICATION nombre_aplicacion**,
APLICACION,
NOMBRE_APLCODE,
DESCRPTION,
AREAFUNC
from admin.VW_APLICACIONES#dblink,
admin.VW_PRODUCTOS#dblink
where **nombre_aplicacion (+) = CODAPLICATION**
)
WHERE 1=1
ORDER BY nombre_aplicacion ASC;
When I try similar type of query with different tables I was getting error as invalid ORA-00904: "NOMBRE_APLICACION": invalid identifier.
If I remove nombre_aplicacion (+) = CODAPLICATION in where condition query is fetching the result. Can any one suggest why I was facing error as its working earlier with sample query and I was getting error? Is this join is valid?
The query is not valid as:
In the inner sub-query you select areafunc and in the outer query you use area which does not appear in the inner sub-query so will not be available.
In the inner sub-query, you define CODAPLICATION to have the alias nombre_aplicacion and then you try to use that alias in the WHERE clause as a join condition; that will not work.
You have not described which column belongs to which table but you want something like:
SELECT DISTINCT
a.codaplication AS nombre_aplicacion,
a.aplicacion,
a.nombre_aplcode,
p.descrption,
p.areafunc
from APLICACIONES a
LEFT OUTER JOIN PRODUCTOS p
ON (a.primary_key_column = p.foreign_key_column)
ORDER BY nombre_aplicacion ASC;
Note: you are going to have to correct the code to give the correct table aliases for each column and give the correct columns for the join condition.

BigQuery select, join from multiple datasets and avoid name conflicts

Imagine I have several datasets and tables.
Format: dataset.table.field
dataset01.table_xxx.field_z
dataset02.table_xxx.field_z
I try to write smth like
select
dataset01.table_xxx.field_z as dataset01_table_xxx_field_z,
dataset02.table_xxx.field_z as dataset02_table_xxx_field_z
from dataset01.table_xxx
join dataset02.table_xxx on dataset02.table_xxx.field_z = dataset01.table_xxx.field_z
to avoid conflicting names
BigQuery says that dataset01.table_xxx.field_xxx is unrecognised name in SELECT clause.
it complains about unrecognised name in join clause too.
Query works if I remove dataset01, dataset02 from SELECT clause and on condition
What is the right way to refer fields in such case?
select
t1.field_z as dataset01_table_xxx_field_z,
t2.field_z as dataset02_table_xxx_field_z
from dataset01.table_xxx t1
join dataset02.table_xxx t2
on t2.field_z = t1.field_z

Regexp in a matching column sql

I have 2 tables. One of them having one of the columns as regexp's. I want to join the two tables on the regexp column. Cannot seem to be able to do this.
To elaborate: table 1 is (errortype, action, error message) and table 2 is(regexp for error message of each type, error code) join columns are error message and regexp, both string. Is this possible? Thanks.
Select * from table1
left outer join table2 on table1.error_message=table2.regExp.
Issue : table2.regExp has actual regular expressions that should match one or more table1.error_messages. the = does not work here.
You need to use the REGEXP or RLIKE operator. = performs exact matches, not pattern matching.
SELECT *
FROM table1
LEFT OUTER JOIN table2
ON table1.error_message REGEXP table2.regexp

Ambiguous Column Name in SQlite3 when attempting do to a select statement with a join clause

It seems like sqlite3 needs additional information in terms of specifying where you want to extract certain columns from when joining two tables. Somehow, when I try to do a select statement with a join clause, the query returns:
Error: ambiguous column name: name
even though I have specified in my statement where I want the 'name' column to be retrieved from, in this case from table t1:
select name from t1 join t2 on t1.name = t2.name where beer = 'Alesmith Decadence' and nickname = 'SUP';
So does sqlite3 not understand this format or something? Or is more information needed for sqlite3 to understand this query.
When you write a query, you should learn to qualify all columns names -- with convenient table aliases. It is unclear what the right aliases are, but something like this:
select t1.name
from t1 join
t2
on t1.name = t2.name
where t2.beer = 'Alesmith Decadence' and t2.nickname = 'SUP';
Then it is clear to you, to everyone else, AND to the SQL parser exactly which columns you are referring to.
Note: You haven't shown sample data or shown the table definitions, so that above may not work because the table aliases may not be correct.
Try This
select t1.name
from t1 join t2 on t1.name = t2.name
where beer = 'Alesmith Decadence' and nickname = 'SUP';

Problem with sql query

I'm using MySQL and I'm trying to construct a query to do the following:
I have:
Table1 [ID,...]
Table2 [ID, tID, start_date, end_date,...]
What I want from my query is:
Select all entires from Table2 Where Table1.ID=Table2.tID
**where at least one** end_date<today.
The way I have it working right now is that if Table 2 contains (for example) 5 entries but only 1 of them is end_date< today then that's the only entry that will be returned, whereas I would like to have the other (expired) ones returned as well. I have the actual query and all the joins working well, I just can't figure out the ** part of it.
Any help would be great!
Thank you!
SELECT * FROM Table2
WHERE tID IN
(SELECT Table2.tID FROM Table1
INNER JOIN Table2 ON Table1.ID = Table2.tID
WHERE Table2.end_date < NOW
)
The subquery will select all tId's that match your where clause. The main query will use this subquery to filter the entries in table 2.
Note: the use of inner join will filter all rows from table 1 with no matching entry in table 2. This is no problem; these entries wouldn't have matched the where clause anyway.
Maybe, just maybe, you could create a sub-query to join with your actual tables and in this subquery you use a count() which can be used later on you where clause.