CTE with Comparison Operator - common-table-expression

I'm getting a "Msg 4104, Level 16, State 1, Line 21
The multi-part identifier "cte.t" could not be bound."
WITH cte AS (SELECT 2*AVG(TOT_ACCT_BAL) t
FROM HSP_ACCOUNT)
SELECT HSP_ACCOUNT_ID,
TOT_ACCT_BAL
FROM HSP_ACCOUNT
WHERE TOT_ACCT_BAL > cte.t
ORDER BY TOT_ACCT_BAL
Can anyone tell me why? cte.t is a single value but looks like it can't be used with the > in WHERE

Try including cte in the join
WITH cte AS (SELECT 2*AVG(TOT_ACCT_BAL) t
FROM HSP_ACCOUNT)
SELECT HSP_ACCOUNT_ID,
TOT_ACCT_BAL
FROM cte cross join HSP_ACCOUNT
WHERE TOT_ACCT_BAL > cte.t
ORDER BY TOT_ACCT_BAL

Related

what is wrong with my query in oracle apex?

The following code does not work, but it does work when I convert it to the next code. Why?
select * ,count(*) over(partition by colour) as counts_by_colour
from bricks;
output:
ORA-00923: FROM keyword not found where expected
modified:
select b.* ,count(*) over(partition by colour) as counts_by_colour
from bricks b;
This is just the way SQL works - nothing to do with APEX.
select * means select all columns from what follows. So...
select * from emp join dept;
...returns all columns from emp and all columns from dept.
You are not allowed to select anything else with select * - e.g. ...
select *, 'abc' from emp;
raises the same error you got.
However, you can use select alias.* to select all columns from one table/view in the query, and then you are also allowed to select other things:
select e.*, 'abc' from emp e;
or
select e.*, d.loc from emp e join dept d;
The "implicit" alias also works:
select emp.*, 'abc' from emp;

Why does Calcite change GROUP_CONCAT to LISTAGG?

I built a RelNode using the following SQL:
SELECT GROUP_CONCAT(ename ORDER BY ename DESC SEPARATOR 'a') FROM emp
and I used RelToSqlConverter to converter it to SQL. I get this SQL:
SELECT LISTAGG(`ename`, 'a') WITHIN GROUP (ORDER BY `ename` IS NULL DESC, `ename` DESC) FROM `emp`
But I want to get GROUP_CONCAT not LISTAGG.
Check https://issues.apache.org/jira/browse/CALCITE-4349
GROUP_CONCAT is analogous to LISTAGG (see CALCITE-2754) (and also to BigQuery and > PostgreSQL's STRING_AGG, see CALCITE-4335). For example, the query
SELECT deptno, GROUP_CONCAT(ename ORDER BY empno SEPARATOR ';')
FROM Emp
GROUP BY deptno
is equivalent to (and in Calcite's algebra would be desugared to)
SELECT deptno, LISTAGG(ename, ';') WITHIN GROUP (ORDER BY empno)
FROM Emp
GROUP BY deptno

Athena: Queries of this type are not supported

I have the current query in athena.
SELECT col1,
col_2,
A.col_3
FROM
(SELECT col_1,
col_3
FROM table_1
JOIN col_3
WHERE col_1 IN
(SELECT DISTINCT col_1
FROM table_2
JOIN table_1
ON table_1.col_1 = table_2.col_1
)
) AS A
LEFT JOIN
(SELECT col_2,
col_3
FROM table_3
JOIN col_3
WHERE col_2 IN
(SELECT DISTINCT col_2
FROM table_2
JOIN table_4
ON table_2.col_1 = table_4.col_1
JOIN table_3
ON table_4.col_2 = table_3.col_2
)
) AS B
ON B.col_3 = A.col_3
Which works in SQLite.
But when I run it in AWS Athena I got the following error:
Queries of this type are not supported (Service: AmazonAthena; Status Code: 400; Error Code: InvalidRequestException; Request ID: some_id)
I assume that some part of this query is not supported by AWS Athena, but I am new to the Framework.
"Queries of this type are not supported" is Athena's generic way of saying that it doesn't understand your SQL, but that it's not a simple syntax error. You're using SQL that Athena does not support, in other words.
Run the innermost part of the query by itself, and if you don't get the error, add the SQL that wraps it, and so on until you find the fragment that causes the error. If you don't know how to fix it ask a new question focused on that.

how to write regex for list of items sharing the same roots

So I have a list of keywords:
['xxxxl','xxxl','xxl','xl','xxxxt','xxxt','xxt','xt']
In bigquery, I want to write a regex, inside the following sql code
SELECT my_column
FROM table
REGEXP_CONTAINS(lower(my_column),regex)
so that my output table contains only the values that don't match any of the items in keywords list.
Thanks
Below is for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.lookup_table` AS (
SELECT ['xxxxl','xxxl','xxl','xl','xxxxt','xxxt','xxt','xt'] keywords
)
SELECT my_column
FROM `project.dataset.table`,
(SELECT STRING_AGG(LOWER(keyword), '|') exclude_pattern
FROM `project.dataset.lookup_table`,
UNNEST(keywords) keyword)
WHERE NOT REGEXP_CONTAINS(LOWER(my_column), exclude_pattern)
You can test / play with above using below simplified example
#standardSQL
WITH `project.dataset.lookup_table` AS (
SELECT ['xxxxl','xxxl','xxl','xl','xxxxt','xxxt','xxt','xt'] keywords
), `project.dataset.table` AS (
SELECT 'xxxxl' my_column UNION ALL
SELECT 'abc'
)
SELECT my_column
FROM `project.dataset.table`,
(SELECT STRING_AGG(LOWER(keyword), '|') exclude_pattern
FROM `project.dataset.lookup_table`,
UNNEST(keywords) keyword)
WHERE NOT REGEXP_CONTAINS(LOWER(my_column), exclude_pattern)
with output
Row my_column
1 abc

Subquery in from of DQL query

I want to translate one of my MySQL query into a DQL one to have a better compatibility of my application.
Here is the query (kinf of) :
SELECT * FROM
(SELECT count(id) as Context FROM context__item) t0
LEFT JOIN (SELECT count(id) Progress FROM progress__family) t1 ON 1
LEFT JOIN (SELECT count(id) Presentation FROM presentation__family) t2 ON 1
LEFT JOIN (SELECT count(id) Discovery FROM discovery__family) t3 ON 1
LEFT JOIN (SELECT count(id) Objection FROM objection__item) t4 ON 1
LEFT JOIN (SELECT count(id) FROM answer__item) t5 ON 1
...
The goal of my query is to have the count of all the lines of each table in my DB into columns (that why the LEFT JOIN is done ON nothing).
I don't find any equivalent to my query or any way to handle it in DQL.