BigQuery Error: Encountered " "OVER" "OVER "" - google-bigquery

I tried to execute this sql statement in BigQuery console but I got an error as below.
Anyone can suggest on this?
"Error: Encountered " "OVER" "OVER "" at line 5, column 99. Was expecting: "END" ..."
SELECT *
FROM (SELECT
*
, CASE
WHEN initiator in ('01_User') THEN RANK() OVER (PARTITION BY item_id, log_date_desc, type ORDER BY log_date_desc DESC)
ELSE RANK() OVER (PARTITION BY item_id, log_date_desc ORDER BY log_date_desc DESC)
END AS ord
FROM temp.step1_itemlogall
) AS t
WHERE ord = 1
Thanks.

I think the issue is with putting analytic functions inside a CASE statement. You can rewrite this as an inner select that computes those two functions, then selects the right one in the outer query. For example:
SELECT *
FROM (
SELECT
CASE WHEN initiator IN ('01_User')
THEN ord1
ELSE ord2
END AS ord
FROM (
SELECT *,
RANK() OVER (PARTITION BY item_id, log_date_desc, type
ORDER BY log_date_desc DESC) as ord1,
RANK() OVER (PARTITION BY item_id, log_date_desc
ORDER BY log_date_desc DESC) as ord2
FROM temp.step1_itemlogall))
WHERE ord = 1
I think the real explanation for that error message is here, however: https://www.youtube.com/watch?v=2OBZf0QdKdE

Switch to STANDARD SQL in the UI or the API call, only the legacy SQL engine throws this error.

Related

oracle db from keyword not found where expected in double cte

I have a double cte expression , the first one join two tables and the second is implementing a partition by function:
with cte as (
select *
from memuat.product p
join memuat.licence l on p.id = l.product_id
where l.managed = 'TRUE'
),
joined as (
select
*,
row_number() over (partition by id order by id) as rn
from cte
)
select * from joined;
I get the following error:
ORA-00923: FROM keyword not found where expected, ERROR at line 12.
I cannot figure out which syntax error is wrong in my query.
Oracle is nitpicking when it comes to SELECT *. SELECT * means "select everything", so how can you possibly add something to it? In Oracle you cannot SELECT *, 1 AS something_else FROM some_table. You must have SELECT some_table.*, 1 AS something_else FROM some_table, so you are no longer selecting "everything", but "everything from the table" :-)
You have
select
*,
row_number() over (partition by id order by id) as rn
from cte
It must be
select
cte.*,
row_number() over (partition by id order by id) as rn
from cte
instead.

MAX TO_DATE OVER two IDs of an IF statement

I’m using the this query to connect with amazon redshift.
And I have the following query:
Select b.*, c."releasedate",
DENSE_RANK() OVER(PARTITION BY b.originboardid ORDER BY TO_DATE(SUBSTRING(b.sprintenddate,0,9), 'DD/Mon/YY') DESC) AS "rank_sprint",
DENSE_RANK() OVER(PARTITION BY b.originboardid ORDER BY TO_DATE(c.releasedate, 'YYYY-MM-DD') DESC) AS "rank_release",
RANK() OVER (ORDER BY b.issueid, b.sprintid DESC) as "rank_issue",
MAX(IF (b.issueorigin='completed') AND (b.changeto='In Progress') and (b.changefield='status')
max(TO_DATE(SUBSTRING(b.changecreation,0,10),'YYYY-MM-DD')) OVER(b.issueid,b.sprintid)
) OVER (b.issueid,b.sprintid) as "lastinprogress"
from digitalplatforms.issues_braze b
Left join jira.releases c
On b.version_id=c.versionid
and its outputing the following error:
[Amazon](500310) Invalid operation: syntax error at or near "max"
Position: 459;
Also if I query just:
Select b.*, c.“releasedate”,
DENSE_RANK() OVER(PARTITION BY b.originboardid ORDER BY TO_DATE(SUBSTRING(b.sprintenddate,0,9), ‘DD/Mon/YY’) DESC) AS “rank_sprint”,
DENSE_RANK() OVER(PARTITION BY b.originboardid ORDER BY TO_DATE(c.releasedate, ‘YYYY-MM-DD’) DESC) AS “rank_release”,
RANK() OVER (ORDER BY b.issueid, b.sprintid DESC) as “rank_issue”
from digitalplatforms.issues_braze b
Left join jira.releases c
On b.version_id=c.versionid
it works.
Can someone help?
Thank you
There is no "IF" statement in SQL. SQL is not procedural. You need to rewrite you query using "CASE" or "DECODE" statements.
Also you cannot nest window functions. If you logic requires this then these need to operate at different levels of the query (SELECT level). However, are you sure both of these need to window functions - MAX() OVER vs. MAX()? They are using the same OVER clause so I expect not.
Just guessing based on you query but does this give you what you want?
MAX(DECODE((b.issueorigin='completed') AND (b.changeto='In Progress') and (b.changefield='status')), true,
TO_DATE(SUBSTRING(b.changecreation,0,10),'YYYY-MM-DD')
) OVER (b.issueid,b.sprintid) as "lastinprogress"

AS400 - Why token *, ! not valid? What could be the alternate way to run the query using STRSQL - SQL Interactive Session?

In AS400, How can I run this query using STRSQL?
For the below query getting the following error message instead returning results.
"Token , was not valid. Valid tokens: FROM INTO."
Code Snippet for Original Query:
WITH cte AS ( SELECT *,
!SUM(status != 'INACTIVE') OVER (PARTITION BY loc_code, user_id, service_area, service_sector) only_inactive,
ROW_NUMBER() OVER (PARTITION BY loc_code, user_id, service_area, service_sector ORDER BY last_changed DESC) rn
FROM test )
SELECT *
FROM cte
WHERE only_inactive AND rn = 1
After checking, the query I found the problem is the SELECT statement inside WITH clause. I don't know why this error is there? Unable to find a possible way to solve this issue.
Now, I tried to remove * from the select statement inside WITH clause, somehow avoided this error. After executing my updated query again, I am getting same kind but a different error message.
Token ! was not valid. Valid tokens: ( + * - ? : DAY INF LAG NAN RID
ROW RRN CASE CAST CHAR DATE DAYS.
Code Snippet for Updated Query:
WITH cte AS ( SELECT !SUM(status != 'INACTIVE') OVER (PARTITION BY loc_code, user_id, service_area,
service_sector) only_inactive,
ROW_NUMBER() OVER (PARTITION BY loc_code, user_id, service_area, service_sector ORDER BY last_changed
DESC) rn
FROM test )
SELECT *
FROM cte
WHERE only_inactive AND rn = 1
I tried:
Instead of ! token I tried using <> and ¬= but it didn't help me in both cases. When I tried to run my code I encountered the same error message but now with <> and ¬= tokens.
Expected Result:
I want to return all records satisfying the logic given in the query with all columns in my table.
Could someone please tell me how to solve this issue?
I tried the following updated query just to test whether it's working without ! or NOT token or not.
WITH cte AS ( SELECT test.*,
SUM(status < 'INACTIVE') OVER (PARTITION BY loc_code, user_id, service_area, service_sector) only_inactive,
ROW_NUMBER() OVER (PARTITION BY loc_code, user_id, service_area, service_sector ORDER BY last_changed DESC) rn
FROM test )
SELECT *
FROM cte
WHERE only_inactive AND rn = 1
I expected this query must have listed some results but this time I get this error message.
Token < was not valid. Valid tokens: ) ,.
I think the problem is with tokens. Not sure what.
Try writing the query like this:
WITH cte AS (
SELECT t.*,
MIN( status = 'INACTIVE') OVER (PARTITION BY loc_code, user_id, service_area, service_sector) as only_inactive,
ROW_NUMBER() OVER (PARTITION BY loc_code, user_id, service_area, service_sector ORDER BY last_changed DESC) as rn
FROM test t
)
Here is a db<>fiddle showing that this syntax works in DB2.
Note that the types of the columns should not matter for the syntax error you are seeing.

My sql statement does not like my partion over statement

I have this simple query in DB2. I'm getting an error at the 2nd line from the top. For someone reason, it does not want to run my over(partition) line?
with core as(
select *,row_number() over(partition by asgnd_to_pin order by stdt asc) as rank
from mhal_rep.stushh
where stus_cd in ('DWRT', 'FINL', 'DWFL', 'DWR', 'DWSR', 'DWPC')
AND STDT BETWEEN '2009-02-28' AND '2019-02-28'
UNION
select *,row_number() over(partition by asgnd_to_pin order by stdt asc) as rank
from mhal_rep.stusha
where stus_cd in ('DWRT', 'FINL', 'DWFL', 'DWR', 'DWSR', 'DWPC')
AND STDT BETWEEN '2009-02-28' AND '2019-02-28'
),
core1 as(
select asgnd_to_pin, stus_cd, stdt, rank, (('2019-02-28'-stdt)/365) as
lngth_srvc
from core
where rank=1 and
asgnd_to_pin in (
'788387',
'271562',
'155851')
select *
from core 1;
The error I'm getting says:
ERROR [42601] [IBM][DB2] SQL0104N An unexpected token "," was found
following "". Expected tokens may include: "FROM INTO".
Use
select t.*, row_number() over(partition by asgnd_to_pin order by stdt asc) as rank
from mhal_rep.stushX t
...
instead of
select *, row_number() over(partition by asgnd_to_pin order by stdt asc) as rank
from mhal_rep.stushX
...
But actually you have more problems:
- core1 subselect is not closed by )
- space between core and 1 in the outer select statement
just remove space between core and 1
select *
from core1;

ERROR [HY000] ERROR: Aggregate function calls may not be nested */

I cannot get the below line to work when adding to my query, any thoughts?
stddev(stat) bias
with c as (
SELECT A.ENGINE_ASOF_CALENDAR_DATE, A.LEVEL_1_CODE CURR, CNTR_TO_ACTIVE_RISK, A.PRICE, a.ACTIVE_WEIGHT_PCT,
lag(a.PRICE, 1) over(partition by a.LEVEL_1_CODE order by a.ENGINE_ASOF_CALENDAR_DATE) price_lag,
lag(CNTR_TO_ACTIVE_RISK, 1) over(partition by a.LEVEL_1_CODE order by a.ENGINE_ASOF_CALENDAR_DATE) risk_lag,
price_lag/a.PRICE - 1 rtn,
a.ACTIVE_WEIGHT_PCT * rtn wgt_rtn
FROM DBS_APPL_RISK_DATAMART.USR_OWNR_RISK_DATAMART.VWC_FOREIGNEXCHANGE_FUND_EXPOSURE A
WHERE A.PORTFOLIO_CODE = 'Sunsuper Active - SUKHH3_Active'
)
SELECT c.*,
sum(wgt_rtn) over(partition by c.ENGINE_ASOF_CALENDAR_DATE)sum_rtn,
sum(risk_lag) over(partition by c.ENGINE_ASOF_CALENDAR_DATE)sum_risk_lag,
sum_risk_lag/sqrt(260) over(partition by c.ENGINE_ASOF_CALENDAR_DATE)sum_lag_risk2,
sum_rtn/nullif(sum_lag_risk2,0) stat,
stddev(stat) bias
FROM c
order by c.ENGINE_ASOF_CALENDAR_DATE desc
You are using column aliases in expressions in the same select where they are defined. You are also mixing aggregation functions in a select that has no group by.
Perhaps this will fix your problem:
WITH . . .
SELECT c.*, sum_rtn / nullif(sum_lag_risk2, 0) as stat,
stddev(sum_rtn / nullif(sum_lag_risk2, 0)) over () as bias
FROM (SELECT c.*,
sum(wgt_rtn) over (partition by c.ENGINE_ASOF_CALENDAR_DATE) as sum_rtn,
sum(risk_lag) over (partition by c.ENGINE_ASOF_CALENDAR_DATE) as sum_risk_lag,
sum_risk_lag/sqrt(260) over (partition by c.ENGINE_ASOF_CALENDAR_DATE) as sum_lag_risk2
FROM c
) c
ORDER BY c.ENGINE_ASOF_CALENDAR_DATE desc
I'm not sure what the standard deviation should be partitioned by. This is over all the data.