SyntaxError when adding and subtracting with BigQuery - google-bigquery

When executing this basic query I have this error:
WITH CALCULO AS (
(SELECT SUM(UTILITY) FROM _coste_rv) - (SUM(UTILITY) + (SELECT SUM(UTILITY) FROM _tmp_coste)))
Message of Error:
Syntax error: Expected ")" but got "-"
What I want it to show is the amount of those 2 values

The top-level statement should be SELECT. I.e. you cannot write
WITH Calculo AS (
foo - bar
)
it should be
WITH Calculo AS (
SELECT foo - bar as some_name
)
i.e.
WITH CALCULO AS (
SELECT
(SELECT SUM(UTILITY) FROM _coste_rv) -
(SUM(UTILITY) + (SELECT SUM(UTILITY) FROM _tmp_coste))
AS some_name
)
SELECT ...

Related

Is it possible to use SQL Update on a Sub Query

Is it possible to use the sql UPDATE on a sub query? I am using Big
Query Standard SQL and have tried every permutation of the below that I can come up with:
WITH test AS (SELECT * FROM 'my.database.table'),
test2 AS (UPDATE test SET myField = 100 WHERE myField < 100)
SELECT * FROM test2
I always receive the error:
Syntax error: Expected "(" or keyword SELECT or keyword WITH but got
keyword UPDATE
Use below instead
with test as (
select * from `my.database.table`
), test2 as (
select * replace(greatest(myfield, 100) as myfield) from test
)
select * from test2

ORA-01745: invalid host/bind variable name Issue

Getting the error for this query which looks valid.
JPA Hibernate native query execution
SELECT
CASE_KEY,
ENCRYPTED_CASE_ID,
FILING_EPOCH_TIME AS CREATION_EPOCH_TIME,
sql2u(SOURCE_LAST_UPDATE_DATETIME) as LAST_UPDATE_EPOCH_TIME
FROM CASE_DIM
WHERE CASE_KEY in (
select * from (
select distinct CASE_KEY
from CASE_TRANSITION_FACT
where CASE_EVENT_KEY BETWEEN :startKey AND :endKey order by CASE_KEY
)
where rownum between :fromRow and :toRow
)

how to run a different select statement based on condition in Hive SQL

I would like to know how to run a different select statement based on condition in Hive SQL.
The following query does not work but throws an error.
Error while compiling statement: FAILED: ParseException line 4:2
cannot recognize input near '(' 'SELECT' '1' in expression
specification
SELECT
CASE WHEN '${UN}'!= '' THEN
(
SELECT *
from table1 t
WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
AND t.un in ('${UN}')
)
ELSE
(
SELECT *
from table1 t
WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
AND t.un in (
(SELECT
o.unq_num as un
FROM table2 as o
WHERE o.date >= '2017-01-01'
AND upper(o.srl_num) in ('${R}')
LIMIT 1)
)
)
END
Use UNION ALL with your queries + add conditions for switching corresponding query:
select *
from table1 t
where (t.yymmddval BETWEEN '${D1}' and '${D2}')
and t.un in ('${UN}')
and '${UN}'!= '' --switching condition
union all
select *
from table1 t
where (t.yymmddval BETWEEN '${D1}' AND '${D2}')
and t.un in
(SELECT
o.unq_num as un
FROM table2 as o
WHERE o.date >= '2017-01-01'
AND upper(o.srl_num) in ('${R}')
LIMIT 1)
and '${UN}'= '' --switching condition

Exception - Combining two data sets in HiveQL

I am facing Analytics Exception while having inner data sets to be combined.
Query:
Select key,days
FROM
(
Select key
FROM sls where id =14004
) AS first
JOIN
(
Select seckey ,days
FROM
(
Select seckey , MAX(opp_days) As days from sls_daily Where id=14004 Group By key
) As f
JOIN
(
Select key,est,cls,days from sls_daily Where dw_cid=14004
) As s
ON f.days = s.days AND f.key= s.key
) AS second
ON second.seckey = first.key
Exception:
AnalysisException: Syntax error in line 15: ) AS first ^ Encountered:
FIRST Expected: IDENTIFIER CAUSED BY: Exception: Syntax error
What is the reason for the error.
Try to avoid reserved words in SQL.
Try like this
Select `key`,`days`
FROM
(
Select `key`
FROM sls where id =14004
) AS `first`
JOIN
(
Select seckey ,`days`
FROM
(
Select seckey , MAX(opp_days) As `days` from sls_daily Where id=14004 Group By key
) As f
JOIN
(
Select `key`,est,cls,`days` from sls_daily Where dw_cid=14004
) As s
ON f.`days` = s.`days` AND f.`key`= s.`key`
) AS `second`
ON `second`.seckey = `first`.`key`

Why does this SQL query that works in MS Access not work in SQL Server?

The following query works in MS Access, but it does not work in MS SQL Server:
SELECT
tblSession.PatientID as PID,
max(tblSession.SessionAttend) -
min(tblSession.SessionAttend) + 1 as NumSA,
max(tblSession.SessionSched) -
min(tblSession.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
tblSession.SessionNumber,
tblSession.SessionDate,
tblSession.SessionAttend,
tblSession.SessionSched FROM
tblPatient INNER JOIN tblSession ON
tblPatient.PatientID = tblSession.PatientID) WHERE
(tblSession.SessionDate >= '12/8/2010') AND
(tblSession.SessionDate <= '5/18/2011') AND
(tblSession.Status = '2') ORDER BY
tblSession.PatientID, tblSession.SessionNumber
) GROUP BY tblSession.PatientID
In SQL Server, it gives the error "Incorrect syntax near the keyword 'GROUP'." When I hover over the GROUP keyword, the tooltip displays "Incorrect syntax near 'GROUP'. Expecting AS, ID, or QUOTED_ID." I don't understand. Can anyone tell me how to make this query work?
The derived table in () needs an alias, and its column references in the SELECT list updated accordingly:
SELECT top 100 percent
ALIASNAME.PatientID as PID,
max(ALIASNAME.SessionAttend) -
min(ALIASNAME.SessionAttend) + 1 as NumSA,
max(ALIASNAME.SessionSched) -
min(ALIASNAME.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
...
...
...
...
tblSession.PatientID, tblSession.SessionNumber
-- This derived table needs an alias
) ALIASNAME
GROUP BY ALIASNAME.PatientID