I have a table with sales records by year as follows:
id year sales
1 2001 10
2 2002 20
3 2003 30
I'm joining the table to itself in order to get a sales_difference from one year to the next:
SELECT s1.*, s1.sales - s2.sales AS sales_difference
FROM sales s1, sales s2
WHERE s1.year = s2.year + 1
This query runs pretty slowly, so I want to create an index on year + 1. According to the PostgreSQL docs you can create indexes on expressions such as:
CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));
So I try doing this:
CREATE INDEX sales_year_plus_one on sales (year + 1);
which gives me:
ERROR: syntax error at or near "+"
LINE 1: ...sales_year_plus_one on sales (year + 1);
^
Why is this particular expression not allowed?