I tried to use a proposed query on Sybase ASE 12, and it complained about syntax error.
SELECT
item,
( SELECT TOP 1 tags.tag
FROM #tags tags
LEFT JOIN t o
ON tags.tag = o.tag
AND o.item_id = n.item_id
WHERE o.tag IS NULL
ORDER BY tags.tag
) 'tag',
value
FROM
t_new n
ERROR: Incorrect syntax near the keyword 'top'.
However, the same query worked when I replaced (TOP 1 tag
... ORDER BY tag
) with MAX():
SELECT
item,
( SELECT max(tags.tag)
FROM #tags tags
LEFT JOIN t o
ON tags.tag = o.tag
AND o.item_id = n.item_id
WHERE o.tag IS NULL
-- ORDER BY tags.tag
) 'tag',
value
FROM
t_new n
Why is using (TOP 1 tag
... ORDER BY tag
) a problem in Sybase's correlated sub queries?
Is there any fix to the original query that does NOT use min()/max()?
A correlated subquery can also be used in the HAVING clause of an outer query. This construction can be used to find the types of books for which the maximum advance is more than twice the average within a given group.
A noncorrelated (simple) subquery obtains its results independently of its containing (outer) statement. A correlated subquery requires values from its outer query in order to execute.
A correlated subquery is a subquery that refers to a column of a table that is not in its FROM clause. The column can be in the Projection clause or in the WHERE clause. In general, correlated subqueries diminish performance.
It is used whenever a subquery must return a different result or set of results for each candidate row considered by the main query. In other words, you can use a correlated subquery to answer a multipart question whose answer depends on the value in each row processed by the parent statement.
Adaptive Server Enterprise version 12.5.3 supports the top n clause in outer query select statements, but not in the select list of a subquery. This differs from Microsoft SQL Server. Any attempt to use the top n clause with Adaptive Server in a subquery yields a syntax error.
From the ASE 12.5.3 documentation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With