0

My query result looks like this:

| A | B |
|-------|
| 1 | 2 |
| 1 | 4 |
| 1 | 6 |
| 1 | 9 |
| 1 | 1 |
| 1 | 6 |
| 1 | 9 |

Now I want to increase column A by the index of the result table, so the result would become like this:

| A | B |
|-------|
| 2 | 2 |
| 3 | 4 |
| 4 | 6 |
| 5 | 9 |
| 6 | 1 |
| 7 | 6 |
| 8 | 9 |

How can I do it? Thanks!

2
  • what do you mean by index? Commented Mar 20, 2019 at 13:30
  • 1
    Why do you want to start from 2? Do you always want to start from 2 or because b starts from 2? Commented Mar 20, 2019 at 13:34

2 Answers 2

2

You want row_number()

select (row_number() over (order by a) + 1) as A, b
from table t;
Sign up to request clarification or add additional context in comments.

1 Comment

There is no need to put an expression between parentheses
0

Maybe something like that:

SELECT 
    (row_number() OVER (ORDER BY A) + A) AS columnAIndex, 
     columnB
FROM ... 

I don't have a PostgreSQL client installed here, therefore, i don't tested this query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.