question

Datazita avatar image
Datazita asked

Using a calculated column in a SQL Select statement

I am trying to write a query. Column A comes from database, column B is either 1 or -1 and it is result of a CASE statement that looks into the same table. Now, my column C needs to refer to column B as part of it's calculation (let's say it is Column_B * Column_X ). How is this done since I cannot use Column_B in my SELECT statement.

querycalculated column
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I thought I understood and then re-read and confused myself! Please can you provide a sample of your code / sample data with an insert statement? I have a couple of thoughts in mind.

0 Likes 0 ·

1 Answer

·
Kev Riley avatar image
Kev Riley answered

Use the full definition of ColumnB in the calculation of ColumnC, so for example if ColumnB was

case when X then -1 when Y then 1 end as ColumnB

then ColumnC would be

(case when X then -1 when Y then 1 end) * ColumnX as ColumnC
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.