PostgreSQL-语法错误-与table_name一起使用时

时间:2018-06-20 12:14:50

标签: postgresql

不知道需要做什么,我使用的是pgadmin3,但是无论GUI如何,我都会遇到相同的错误。

以下是我的查询:

 with base_table as 
(select row_number() over (partition by p.customer_id order by p.payment_date ) as early_order,
        row_number() over (partition by p.customer_id order by p.payment_date desc) as last_order
    from payment p)
  

输入结尾处的语法错误

我什至尝试在末尾添加分号,但不起作用:

with base_table as 
(select row_number() over (partition by p.customer_id order by p.payment_date ) as early_order,
        row_number() over (partition by p.customer_id order by p.payment_date desc) as last_order
    from payment p);
  

“;”附近的语法错误

内部查询(如下)工作正常:

select row_number() over (partition by p.customer_id order by p.payment_date ) as early_order,
        row_number() over (partition by p.customer_id order by p.payment_date desc) as last_order
    from payment p

只有当我使用with table_name as时,错误才开始出现。

1 个答案:

答案 0 :(得分:4)

您需要添加主要选择

with base_table as (
 select row_number() over(partition by p.customer_id order by p.payment_date) as early_order,
        row_number() over(partition by p.customer_id order by p.payment_date desc) as last_order
 from payment p)
SELECT *
FROM base_table