My sql statement does not like my partion over statement - sql

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;

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.

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.

How to retriever the First Record and Last Record in the Group, for Same Date, In Oracle SQL

Please Suggest me, I have tried with Partition and group in Oracle SQL, but unable to get the right query
One option uses row_number() twice:
select primary_key, id, created_date
from (
select
t.*,
row_number() over(partition by id, trunc(created_date) order by created_date asc) rn_asc,
row_number() over(partition by id, trunc(created_date) order by created_date desc) rn_desc
from mytable t
) t
where 1 in (rn_asc, rn_desc)

BigQuery Error: Encountered " "OVER" "OVER ""

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.

PostgreSQL query works locally (9.x), but gives syntax error on Heroku (8.x)

I developed my site and it works like a charm on my local machine. The query is supplied by a fellow Stackoverflow member in this thread.
schema.rb portion that is relevant
Same data locally as on Heroku.
select *
from (
select *
, row_number() over (partition by odds_type order by odds_index desc) as rn2
from (
select *
, row_number() over (partition by event_id, bookmaker_id, odds_type
order by created_at desc) as rn1
from Odds
where event_id = #{e.id}
) sub1
where rn1 = 1
) sub2
where rn2 = 1
The error in the console
Odds Load (88.4ms) select * from ( select *, row_number() over (partition by odds_type order by odds_index desc) as rn2 from (select *, row_number() over (partition by event_id, bookmaker_id, odds_type order by created_at desc) as rn1 from Odds where event_id = 21 ) sub1 where rn1 = 1 ) sub2where rn2 = 1
ActiveRecord::StatementInvalid: PG::Error: ERROR: syntax error at or near "over"
LINE 1: select * from ( select *, row_number() over (partition by od...
^
row_number() and window functions in general are available in PostgreSQL 8.4 or higher. The error indicates that you're trying this query on an older version (8.3?), so it can't work.
Update: after studying the original question and answer with PG 9.x, I believe you could obtain the same result with the PostgreSQL-specific DISTINCT ON clause, non-standard but available in older versions of PG and quite handy in your case. Here's my proposal:
SELECT DISTINCT on (ot) bi,ot,oi FROM
(select distinct on (bi,ot) bi,ot,oi from odds
where ei=1 order by bi,ot,created_at desc) subq
ORDER BY ot,oi DESC;