0

I have the follwing SQL, and It's giving me psycopg2.ProgrammingError: column "rownum" does not exist.

SELECT *, (case when col1 = 'A' then
                    1
           when col1 = 'B' then
                    2
           else
                    3
           end) as rownum
from table1 
order by colb, rownum, colc 
user1187968
  • 7,154
  • 16
  • 81
  • 152

1 Answers1

3

Some RDBMS's do not recognize aliased columns in the order by clause. You can subquery what you have:

SELECT * FROM (SELECT *, ... AS rownum FROM table1) AS A ORDER BY colb, rownum, colc

In Postgresql, I believe you can also do:

SELECT (case ...) as rownum, * FROM table1 ORDER BY colb, 1, colc
zambonee
  • 1,599
  • 11
  • 17