ParseException: SQL CTE - apache-spark-sql

result = aml_identity_g.connectedComponents()
conn_comps = result.select("id", "component",'type') \
.createOrReplaceTempView("components")
display(result)
Which creates
%sql
create table temptable
as with dupes as (
select component, count(case when type = 'Person' then 1 end) person_ct
from components
group by component
having person_ct > 1
)
Throws me an error as
Error in SQL statement: ParseException:
mismatched input '<EOF>' expecting {'(', 'DESC', 'DESCRIBE', 'FROM', 'MAP', 'REDUCE', 'SELECT', 'TABLE', 'VALUES'}(line 6, pos 21)
== SQL ==
create table temptable
as with dupes as (
select component, count(case when type = 'Person' then 1 end)
person_ct
from components
group by component
having person_ct > 1
)
---------------------^^^
Don't understand the error here.

You really don't need CTE in this query - it should be enough to have normal SELECT. So it should be something like this:
create table temptable as (
select component, count(case when type = 'Person' then 1 end) person_ct
from components
group by component
having person_ct > 1
)
P.S. According to documentation on HAVING clause you can use alias in the HAVING expression.

Related

Hive query to find conversion ratio

I am trying this query in Hive and it's not working.
select
(
(
select
count(*)
from
click_streaming
where
page_id= 'e7bc5fb2-1231-11eb-adc1-0242ac120002'
and is_page_view = 'Yes'
) / (
select
count(*)
from
click_streaming
where
button_id= 'fcba68aa-1231-11eb-adc1-0242ac120002'
and is_button_click= 'Yes'
)
) as conversion_ratio;
Error I am getting: cannot recognize input near 'select' 'count' '(' in expression specification
I am basically trying to get conversion rate of customers who view the page and click the button to book a cab.
This is not how the syntax can be. Just join them both or use a case when to do your job.
select
sum(case when page_id= 'e7bc5fb2-1231-11eb-adc1-0242ac120002' and is_page_view = 'Yes' then 1 else 0 end) /
sum(case when button_id= 'fcba68aa-1231-11eb-adc1-0242ac120002' and is_button_click= 'Yes' then 1 else 0 end) conv_ratio
FROM
click_streaming
or you can reuse your SQLs but you got to join them
select c1/c2
from (
select
count(*) c1
from
click_streaming
where
page_id= 'e7bc5fb2-1231-11eb-adc1-0242ac120002'
and is_page_view = 'Yes') rs
join (select
count(*) c2
from
click_streaming
where
button_id= 'fcba68aa-1231-11eb-adc1-0242ac120002'
and is_button_click= 'Yes')rs2

Returning something like error-codes from query

I want to check conditions in query. If condition is false, then query must returns error code, else query must to execute another query.
Something like that:
WITH error_code_get AS (
SELECT
CASE
WHEN NOT EXISTS (
SELECT 1
FROM users
WHERE id = '1a4b...'
) THEN 1
WHEN NOT EXISTS (
SELECT 1
FROM workspaces
WHERE id = '353...'
) THEN 2
WHEN (
SELECT settings
FROM workspace_roles
WHERE workspace_id = '353...'
AND id IN (
SELECT role_id
FROM m2m_users_to_workspace_or_projects_roles
WHERE role_type='1'
AND user_id='1a4b...'
)
) < 2 THEN 3
ELSE 0
END error_code
RETURNING error_code
)
// WRONG PART
CASE
WHEN (SELECT error_code FROM error_code_get) = 0 THEN (INSERT INTO x(a) VALUES('some_value'))
ELSE (SELECT error_code FROM error_code_get)
END
You can't do something like that in pure SQL, you'd need to write code in Stored Proc or Function

Why Would Unknown Column Be Referenced in SQL Query

I am in the process of updating some SQL queries to run against MariaDB instead of via SQL Anywhere. One query I'm running is erroring with this:
Error Code: 1054. Unknown column 'choice' in 'field list'
That is for this query:
SELECT
(select firstname||' '||lastname||' ('||service||')' from staff_members where id_number = customer_assignment_reviews.staff_member_id) as Rep,
(select customer_firstname||' '|| customer_lastname from customers where id_number = customer_assignment_reviews.cs_id) as Cus,
last_modified as "Response Date",replace(review_reason,'’','') as "Reason",
(Select choice = CASE
when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end) as "Accepted?"
FROM customer_assignment_reviews
where staff_member_id in (Select id_number from kar.staff_members where division_id = 6)
and "Response Date" between today() - 7 and today() /* Date Range */
and "Accepted?" = 'No'
Order by 3 desc
Is this error message as straightforward as it sounds? It's simply saying the column "choice" doesn't exist on the target table?
I'm just trying to reason through why this code (which I inherited) would be referencing a column that does not exist. Could something be expected here at runtime?
You don't need to use subquery in SELECT list
SELECT
-- ...
(Select choice = CASE
when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end) as "Accepted?"
=>
SELECT
CASE
when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end as "Accepted?"
Additionaly syntax SELECT alias = expression is only T-SQL specific:
SELECT alias = 1
<=>
SELECT 1 AS alias
What is this supposed to mean?
(Select choice = CASE
when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end) as "Accepted?"
Very importantly, a select is not needed here. You might mean:
(case when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end) as is_accepted -- prefer to not have to need escape characters
If accepted only takes those two values, you can simplify this to:
elt(accepted + 1, 'No', 'Yes') as is_accepted

ORACLE: USE RESULT OF CASE-WHEN-STATEMENT

I have a huge query and I am wondering if it is in Oracle possible
to get the result of a case-when-statement and use it for comparison? My CASE-STATEMENT is declared in the Select-Statement and it looks like this.
SELECT........
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST;
Now I want to get the result of this case-statement and use it in the where part? Is it possible? (Sry this may be a dumb question)
If you define your CASE statement in either an inline-view or a common table expression (aka WITH clause), you can refer to it by whatever alias you give it.
For example (inline-view):
SELECT ...
FROM ( SELECT .....
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST
FROM...
) v
WHERE v.test = 'TEST2';
As a common table expression, it would be:
WITH cte AS ( SELECT........
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST
FROM ... )
SELECT ...
FROM cte
WHERE test = 'TEST2';
You can use a case statement in the where clause, for eg.:
select * from table
where table.field = (CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END)
This will compare the value returned from the case statement with the table field.

UNION ALL / UNION on Presto

I am using treasure data for data analytics and having trouble with union statement in presto db.
How do i do a Union All on presto. I dont understand the documentation. everytime I try to do UNION like so:
SELECT
COUNT(*) AS ReservationsCreated,
resource
FROM
reservation
WHERE
type = 'create'
UNION
SELECT
COUNT(*) AS ReservationsDeleted,
resource
FROM
reservation
WHERE
type = 'delete'
GROUP BY
resource
;
I get output reformatted like:
SELECT
COUNT(*) AS ReservationsCreated,
resource
FROM
reservation
WHERE
type = 'create'
UNION
SELECT
COUNT(*) AS ReservationsDeleted,
resource
FROM
reservation
WHERE
type = 'delete'
GROUP BY
resource
;
and error that says:
'"resource"' must be an aggregate expression or appear in GROUP BY clause
I think I am not understanding the syntax for Presto. The docs are very confusing on Union. Any help appreciated.
The first part of the query is missing a group by as the error says.
SELECT COUNT(*) AS ReservationsCreated, resource
FROM reservation
WHERE type = 'create'
group by resource
UNION ALL
SELECT COUNT(*) AS ReservationsDeleted, resource
FROM reservation
WHERE type = 'delete'
GROUP BY resource
In fact, the query could be simplified to use conditional aggregation.
select
resource
,sum(case when type = 'create' then 1 else 0 end) as reservationscreated
,sum(case when type = 'delete' then 1 else 0 end) as reservationsdeleted
from reservation
group by resource