sql - 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".
解决方案
利用
select t.*, row_number() over(partition by asgnd_to_pin order by stdt asc) as rank
from mhal_rep.stushX t
...
代替
select *, row_number() over(partition by asgnd_to_pin order by stdt asc) as rank
from mhal_rep.stushX
...
但实际上你还有更多问题:
- core1
subselect 不是由-外部 select 语句)
之间core
和中的空格1
推荐阅读
- javascript - 谁能明白为什么这个javascript函数不起作用?
- python - Django Rest Framework 对象没有属性 pk
- sql - 选择连接
- sql - 给我的数据库中的字段一个特定的数字
- c++ - 如何像opengl一样将厚度计算为固定宽度?
- express - 动态设置随机数
与快速静态? - mysql - 将 Mysql 表从 MyISAM 更改为 InnoDB
- r - 如何使用 purrr::map 为列表中的每个数据帧求和 nas
- flutter - Flutter如何命名一个函数然后将它传递给另一个函数?
- php - 从下拉列表中更新所选值的 MySQL 记录