This question already has an answer here:
SQL - Unequal left join BigQuery
(1 answer)
Closed 4 years ago.
I want to have a left join in bigquery.
SELECT id,mtr,name FROM (SELECT userid,mtr,name FROM
results_20180612_230337
LEFT JOIN table1 ON id=myid where
partitiondate=CAST("2018-05-29" AS DATE)) LEFT JOIN `table2` t2
ON
(CASE
WHEN(SUBSTR(name,1,6)='000000') THEN mtr = CAST(bccbnc AS STRING)
WHEN(CHAR_LENGTH(CAST(bccbnc AS STRING))>5) THEN SUBSTR(name,1,6) =
CAST(bccbnc AS STRING)
ELSE SUBSTR(name,1,5) = CAST(bccbnc AS STRING)
END)
I am getting error
Error: LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
It works if I remove CASE.
BigQuery, unlike MySQL is not very good at joining on complex relationships.
I suggest you create an intermediary/temporary (see here) table where you can then join on a equality (even a view works for those matters)
You can create a column that uses that case statement:
CASE
WHEN(SUBSTR(name,1,6)='000000') THEN mtr
WHEN(CHAR_LENGTH(CAST(bccbnc AS STRING))>5) THEN SUBSTR(name,1,6)
ELSE SUBSTR(name,1,5) end as column_to_join_onbccbnc
And then on join on it
Related
This question already has answers here:
How do I convert a "legacy" left outer join statement in Oracle?
(1 answer)
Complex Left Outer Joins in Oracle, converting to PostgreSQL
(1 answer)
Convert Oracle legacy outer join to Ansi SQL
(2 answers)
Convert Oracle outer join to SQL Server
(1 answer)
In Oracle, in regards to syntax - how do I convert the (+) syntax to modern conventional JOIN?
(2 answers)
Closed 5 years ago.
I need to implement the old style SQL outer join with condition to the new style.
I am unable to convert the last line of the Join with condition.
Old Style Query:
SELECT cpd.customer_ref, cpd.product_seq, bci.contract_inst_id,
bci.start_dat bci_start_dat, bci.end_dat bci_end_dat
FROM custproductdetails cpd, balcontractinstance bci
WHERE cpd.customer_ref = bci.customer_ref(+)
AND cpd.contract_seq = bci.contract_seq(+)
AND cpd.end_dat >= bci.start_dat(+) AND cpd.end_dat <= bci.end_dat(+);
I assume you are going from the oracle syntax to sqlserver etc, which requires the ansi syntax, which is better to use going forward in general, regardless of database
Select a.a,b.a
From a left join b on a.a = b.a
Is the same as
Select a.a, b.a
From a, b
Where a.a = b.a(+)
I believe this does what you want:
SELECT cpd.customer_ref, cpd.product_seq, bci.contract_inst_id,
bci.start_dat bci_start_dat, bci.end_dat bci_end_dat
FROM custproductdetails cpd LEFT JOIN
balcontractinstance bci
ON cpd.customer_ref = bci.customer_ref AND
cpd.contract_seq = bci.contract_seq AND
cpd.end_dat >= bci.start_dat AND
cpd.end_dat <= bci.end_dat;
The general rule is that the table referenced with the (+) gets additional rows added. That makes it the second table in the LEFT JOIN.
Old Style Query:
SELECT cpd.customer_ref, cpd.product_seq, bci.contract_inst_id,
bci.start_dat bci_start_dat, bci.end_dat bci_end_dat
FROM custproductdetails cpd, balcontractinstance bci
WHERE cpd.customer_ref = bci.customer_ref(+)
AND cpd.contract_seq = bci.contract_seq(+)
AND cpd.end_dat >= bci.start_dat(+)
AND cpd.end_dat <= bci.end_dat(+);
'New' Style Query:
SELECT cpd.customer_ref, cpd.product_seq, bci.contract_inst_id,
bci.start_dat bci_start_dat, bci.end_dat bci_end_dat
FROM custproductdetails cpd LEFT JOIN balcontractinstance bci
ON cpd.customer_ref = bci.customer_ref
AND cpd.contract_seq = bci.contract_seq
AND cpd.end_dat >= bci.start_dat
AND cpd.end_dat <= bci.end_dat;
As you can see it's pretty similar.
This question already has answers here:
How do I create a table based on another table [duplicate]
(2 answers)
Closed 6 years ago.
everyone! I would like to save the table after I used INNER JOIN.
CREATE TABLE result AS (
SELECT measures.Day,
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure)
But I have an error "Invalid syntax next to ( ". Why?
Thank you in advance :)
You need to use the INTO clause, see example below:
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit INTO result
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit
INTO result
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure
I have the following SQL query, and need to figure out where the "signatures" data is actually being read from. It's not from the 'claims' table, and doesn't seem to be from the 'questionnaire_answers' table. I believe it will be a boolean value, if that helps at all.
I'm reasonably proficient at SQL, but the joins have left me a bit confused.
(There's some PHP, but it's not relevant to the question).
$SQL="SELECT surveyor, COUNT(signed_total) AS 'total', SUM(signed_total) AS 'signed_total' FROM (
SELECT DISTINCT claims.claim_id, CONCAT(surveyors.user_first_name, CONCAT(' ', surveyors.user_surname)) AS 'surveyor', CASE WHEN signatures.claim_id IS NOT NULL THEN 1 ELSE 0 END AS 'signed_total' FROM claims
INNER JOIN users surveyors ON claims.surveyor_id = surveyors.user_id
LEFT OUTER JOIN signatures ON claims.claim_id = signatures.claim_id
INNER JOIN questionnaire_answers ON questionnaire_answers.claim_id = claims.claim_id
WHERE (claims.claim_type <> ".$conn->qstr(TYPE_DESKTOP).")
AND (claims.claim_type <> ".$conn->qstr(TYPE_AUDIT).")
AND (claims.claim_cancelled_id <= 0)
AND (claims.date_completed BETWEEN '".UK2USDate($start_date)." 00:00:00' AND '".UK2USDate($end_date)." 23:59:59')
) AS tmp
GROUP BY surveyor
ORDER BY surveyor ASC
";
Thank you!
signatures is a table (see LEFT OUTER JOIN signatures in your query).
As written in FROM clause :
FROM claims
INNER JOIN users surveyors ON claims.surveyor_id = surveyors.user_id
LEFT OUTER JOIN signatures ON claims.claim_id = signatures.claim_id
The LEFT keyword means that the rows of the left table are preserved; So all rows from claims table are considered and NULL marks are added as placeholders for the attributes from the nonpreserved side of the join which is signatures table here.
So CASE WHEN signatures.claim_id IS NOT NULL THEN 1 ELSE 0 END AS 'signed_total' basically checks that if a match between these two tables exists based on claim_id then signed_total column should have value 1 else 0.
Hope that helps!!
This question already has answers here:
Oracle "(+)" Operator
(4 answers)
Closed 9 years ago.
We tried to migrate from Oracle to Postgres. We use ora2pg , but we have an error with this code:
SELECT DISTINCT UPU.USUA_C_USUARIO
FROM GN_USUARIOS U,TR_USUARIOS_X_PERFILES_USUARIO UPU,TR_V_PERFILES_USUARIOS PU
WHERE (U.C_USUARIO = UPU.USUA_C_USUARIO(+))
AND (UPU.PEUS_X_PEUS = PU.X_PEUS)
AND U.C_USUARIO = USU.C_USUARIO))
OR NOT EXISTS (
SELECT UPU2.USUA_C_USUARIO
FROM TR_USUARIOS_X_PERFILES_USUARIO UPU2
WHERE UPU2.USUA_C_USUARIO = USU.C_USUARIO)
OR USER = (
SELECT V_CONSTANTE
FROM GN_CONSTANTES
WHERE C_CONSTANTE = 'TRUSUPROP')
We have an error with PU.USUA_C_USUARIO(+). We dont have enough experience in this kind of conversions. How can we transform the code with LEFT OUTER JOIN ?
Thanks!
Probably like so:
FROM GN_USUARIOS U
LEFT JOIN TR_USUARIOS_X_PERFILES_USUARIO UPU ON U.C_USUARIO = UPU.USUA_C_USUARIO
LEFT JOIN TR_V_PERFILES_USUARIOS PU ON UPU.PEUS_X_PEUS = PU.X_PEUS
WHERE U.C_USUARIO = USU.C_USUARIO)
Or possibly:
FROM GN_USUARIOS U
LEFT JOIN ( SELECT UPU.USUA_C_USUARIO, ...
FROM TTR_USUARIOS_X_PERFILES_USUARIO UPU
JOIN TR_V_PERFILES_USUARIOS PU ON UPU.PEUS_X_PEUS = PU.X_PEUS
) UPU_PU ON U.C_USUARIO = UPU_PU.USUA_C_USUARIO
WHERE U.C_USUARIO = USU.C_USUARIO)
In either case, be wary that there's a USU table that is defined nowhere and mismatched parenthesis in your query.
This question already has answers here:
SQL: JOIN syntax error
(2 answers)
Closed 9 years ago.
I am attempting to eliminate unwanted duplicate query results. The gist is that the field [CUSIP] exists in all tables in question, however, the field [4DTYR] exists in all tables except [IDX_FS].
I had previously only joined the tables via the [CUSIP] field, and that resulted in the query produced unwanted duplicate results (some sort of a permutation of [4DTYR] from all the tables that contained that field).
Then, I made the modification below. However, now I'm receiving a JOIN syntax error. Can anyone kindly help? I've reposted just in case this Q got a little bit stale. Thanks!
FROM
(((IDX_FS LEFT JOIN DATA_BS
ON IDX_FS.CUSIP = DATA_BS.CUSIP)
LEFT JOIN DATA_Footnotes
ON IDX_FS.CUSIP = DATA_Footnotes.CUSIP)
LEFT JOIN DATA_IS
ON IDX_FS.CUSIP = DATA_IS.CUSIP)
LEFT JOIN DATA_SP
ON IDX_FS.CUSIP = DATA_SP.CUSIP
AND (((DATA_BS LEFT JOIN DATA_IS
ON DATA_BS.CUSIP = DATA_IS.CUSIP
AND DATA_BS.4DTYR = DATA_IS.4DTYR)
LEFT JOIN DATA_SP
ON DATA_BS.CUSIP = DATA_SP.CUSIP
AND DATA_BS.4DTYR = DATA_SP.4DTYR)
LEFT JOIN DATA_Footnotes.4DTYR
ON DATA_BS.CUSIP = DATA_Footnotes.CUSIP
AND DATA_BS.4DTYR = DATA_Footnotes.4DTYR
Looks like your error is here
LEFT JOIN DATA_Footnotes.4DTYR -- this is not a valid table name
ON DATA_BS.CUSIP = DATA_Footnotes.CUSIP
AND DATA_BS.4DTYR = DATA_Footnotes.4DTYR
removing the .4DTYR should fix your error
LEFT JOIN DATA_Footnotes
ON DATA_BS.CUSIP = DATA_Footnotes.CUSIP
AND DATA_BS.4DTYR = DATA_Footnotes.4DTYR