Reputation: 21
In Excel if I have say numbers 1, 2 and 3 in columns A, B and C. I can write a formula in column D "=A+B" and then a formula in column E "=D+C".
Basically, I can use the result of a calculated column in the same row.
Can I achieve something similar in SQL with a single line of query.
For example, something like
SELECT A, B, C, A+B as D, D+C as E
FROM TABLE1
Result: 1, 2, 3, 3, 6
Upvotes: 2
Views: 575
Reputation: 33381
You can use calculated columns when create table as
CREATE TABLE tbl(id int, A int, B int, C int, D as A+B, E as A + B + C);
insert tbl(A, B, C) values (1, 2, 3)
Or use
SELECT A, B, C, A+B as D, + A+B + C as E
FROM TABLE1
Upvotes: 1