getting in snowflake SQL compilation error: Unsupported subquery type cannot be evaluated. while using lookup table in query
like--
select
field1,
(select
L.FIELD_DESC
from "STG"."LKP_CD_DESC" L
where S.field2 = L.FIELD_CD and FIELD_NAME='ABC'
) as field_DESC
from "STG"."table1" S;
This particular query could be rewritten as LEFT JOIN:
SELECT S.field1,
L.FIELD_DESC
FROM "STG"."table1" AS S
LEFT JOIN "STG"."LKP_CD_DESC" AS L
ON S.field2=L.FIELD_CD
AND L.FIELD_NAME='ABC';
The only difference is when it exists more than one FIELD_DESC per join conditions:
original subquery would return error as scalar subqueries cannot return more than one row
LEFT JOIN "duplicates" row from S table
Snowflake has some limitations with subqueries.
https://docs.snowflake.com/en/user-guide/querying-subqueries.html#types-supported-by-snowflake
Better to rewrite the query using joins
Related
I'm recently studying SQL Server. I read a book and it says that merge join requires at least one equal sign(=) on ON clause in SQL Server.
So, I tried this query below and I found that the error occurs.
SELECT *
FROM TABLE_1 AS T1
INNER MERGE JOIN TABLE_2 AS T2
ON T1.COL_1 > T2.COL_2
Error Message:
Msg 8622, Level 16, State 1, Line 6 Query processor could not produce
a query plan because of the hints defined in this query. Resubmit the
query without specifying any hints and without using SET FORCEPLAN.
And this book also says this can be done in case of Full Outer Join.
So I tried this query below and found it committed successfully with no errors.
SELECT *
FROM TABLE_1 AS T1
FULL OUTER MERGE JOIN TABLE_2 AS T2
ON T1.COL_1 > T2.COL_2
I tried to search for the reason but I couldn't find any explanation about this.
Can anyone tell me why SQL Server doesn't allow merge join without an equality operator unless it's full outer join?
Thank you for reading my question
You used Inner Merge Join. Merge clause is a join hint that tells sql engine to work more efficient.
Merge joins have the fastest algorithm since each row only needs to be read once from the source inputs. Also, optimizations occurring in other join operators can give those operators better performance under certain conditions.
if you must use '>' operator, use regular Inner Join, like this.
SELECT *
FROM TABLE_1 AS T1
INNER JOIN TABLE_2 AS T2
ON T1.COL_1 > T2.COL_2
I'm trying to do this in a SQL Server CE database, but the database engine keeps reporting errors.
SELECT C.guid, C.name, C.updated,
C.hddsize, C.hddavailable, C.hddfree,
C.ramsize, C.profiles, C.cpu,
(SELECT COUNT(D.id) AS numprogs
FROM ComputerData AS D
WHERE D.computer_id = C.id) AS numprograms
FROM Computers AS C;
I've been told SQL Server CE supports subqueries. Is there something I'm doing wrong?
The limitation in SQL CE is that it does not support subqueries that return a scalar value. Subqueries that return a set are parsed fine.
The subquery in the join in Grayson's answer returns a set, so it should work. Sometimes a scalar subquery cannot be avoided in a join condition. By using 'IN' instead of '=', the parser can be tricked.
See my answer to this question.
My only experiences in queries are with MySQL, but hopefully it is similar enough.
Your query looks strange to me because your subquery is in the SELECT clause. I have never seen that before... but apparently it is supported in MySQL. Usually the subquery comes in the after a FROM or LEFT JOIN or JOIN.
Your example is simple enough that you could implement it with a LEFT JOIN:
SELECT C.guid, ..., COUNT(distinct D.id) as numprogs
FROM Computers AS C
LEFT JOIN ComputerData as D ON D.computer_id = C.id
In this case, LEFT JOIN is the correct type of join to use because even if there is no matching record in the D table for a particular C record, your result set will still contain that C record and numprogs will just be zero, as you would expect.
If you really want to use a subquery, try this:
SELECT C.guid, ..., S.numprogs
FROM Computers AS C
LEFT JOIN
(SELECT computer_id, COUNT(*) as numprogs
FROM ComputerData GROUP BY computer_id) AS S
ON C.id=S.computer_id
I suggest simplifying your query to get it to be the simplest possible query that should work, but doesn't work. Then tell us the specific error message that your database engine is returning.
Edit: I loooked in the MySQL chapter about subqueries and it seems like you should try removing the "as numprograms" clause after your subquery... maybe you don't get any choice about the naming of the column that comes out of the subquery after you've already composed the subquery.
I want to select some rows from a catalog which are not related to two table (Table1 and Table2).
The rows in the catalog are related to the tables using the field idA.
I am using the following query:
;with CTE(id) AS(
select distinct idA from Table1
union
select distinct idA from Table2
)
select * from Catalog where IdA NOT IN (select id from CTE)
The very strange part is that this query does not return any result.
If I use the following query after de CTE it will return some rows
select Id from Catalog where not exists (select 1 from CTE where Catalog.IdA = CTE.Id);
Does anyone know what is the reason of this? AFAK both queries are equivalents.
Of course the first query is slower than the second one but that is not very important. The important part is, why those queries do not return the same results?
Maybe it's important to mention that Table1 has more than 3.9 million rows
but just 139,283 different values for idA.
Table2 has only some thousands rows
Any help or comment will be appreciated
If one or more NULL values are returned by a NOT IN subquery, the result of the predicate is unknown rather than true or false and no rows returned due to the unsatisfied condition. It is best to avoid NOT IN with nullable expressions to avoid this non-intuitive behavior.
Although slighly more verbose, a correlated NOT EXISTS subquery will always return true or false and avoid the NULL gotcha.
SELECT Product.prodName, Runs.buildNumber, Runs.prodDate
FROM Product
INNER JOIN Runs on prodId where Runs.runId=118
The above is my query for the schema:
[Please refer comment.It doesn't let me post images here]
It gives me an error : An expression of non-boolean type specified in a context where a condition is expected
Small syntax error is there
Inner Join Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name where condition;
Update Your query as follows
SELECT Product.prodName, Runs.buildNumber, Runs.prodDate
FROM Product
INNER JOIN Runs on Product.columnname=Runs.columnname where Runs.runId=118
Just Go to this link there is a clean and neat example of joins
You query needs to be like this
SELECT Product.prodName, Runs.buildNumber, Runs.prodDate
FROM Product
INNER JOIN Runs r on p.prodId=r.prodId where Runs.runId=118
Your syntax is wrong:
The on condition must be a boolean condition.
SELECT Product.prodName, Runs.buildNumber, Runs.prodDate
FROM Product
INNER JOIN Runs on prodId=... where Runs.runId=118
So it must be <column from Product> = <column from Runs>
From Itzik Ben-Gan's book Microsoft SQL Server 2012 T-SQL Fundamentals
INNER JOINapplies a Cartesian product between the two input tables as in a cross join, and then it filters rows based on a predicated that you specify.
Based on above description, it must be predicate expression after ON, such as Product.prodId=Runs.prodId
I was having a problem with a larger query in SQL Server which I traced back to this section of code which isn't performing as expected.
SELECT item_name,item_num,rst_units
FROM tbl_item left join tbl_sales_regional on item_num=rst_item
WHERE rst_month=7 and rst_customer='AB123'
The first table (tbl_item) has 10,000 records. The second table (tbl_sales_regional) has 83 for the shown criteria.
Instead of returning 10,000 records with 9,917 nulls, the execution plan shows SQL Server has rewritten as an inner join and consequently returns 83 results.
In order to achieve the intended result, I have to join off a subquery. Can someone provide an explanation why this doesn't work?
Not sure which fields belong where, but you seem to have some fields from tbl_sales_regional in your WHERE condition.
Move them into the ON clause:
SELECT item_name, item_num, rst_units
FROM tbl_item
LEFT JOIN
tbl_sales_regional
ON rst_item = item_num
AND rst_month = 7
AND rst_customer = 'AB123'
The WHERE clause operates on the results of the join so these conditions cannot possibly hold true for any NULL records from tbl_sales_regional returned by the join, as NULL cannot be equal to anything.
That's why the optimizer transforms your query into the inner join.
Any conditions you have in your WHERE clause are applied regardless of the left join, effectively making it an inner join.
You need to change it to:
SELECT item_name,item_num,rst_units
FROM tbl_item left join tbl_sales_regional on item_num=rst_item
AND rst_month=7 and rst_customer='AB123'