Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql - Syntax error - while using with table_name as

Tags:

postgresql

Not sure what needs to be done , i am using pgadmin3 but regardless of the GUI i keep getting the same error .

Below is my query :

 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)

Syntax error at the end of input

I have even tried adding a semicolon at the end, doesnt work :

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);

Syntax error near ";"

The inner query (which is below) works just fine :

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

Its only when i use with table_name as , that the errors start to show up.

like image 606
user1411837 Avatar asked Apr 19 '26 22:04

user1411837


1 Answers

You need to add main select:

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
like image 110
Lukasz Szozda Avatar answered Apr 21 '26 11:04

Lukasz Szozda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!