Daniel Neel
Daniel Neel

Reputation: 1247

Referencing a column in a SELECT from another column in that SELECT

I haven't found a way to do this yet, but I figured I'd check in case I'm missing something.

What I'm wanting to do is basically:

SELECT st.SomeIntValue AS Column1, Column1 * 10 AS Column2, Column2 * 3 AS Column3
FROM SomeTable st

Is there some way to do this in SQL Server, without resorting to nested sub-queries?

Edit: Example to make it more clear why I'm doing this. I need to do this:

SELECT CASE WHEN (ThisThingIsTrue
                  AND ThisOtherThingIsTrue) THEN 1
                                            ELSE 0
       END AS Concept1,
       CASE WHEN (ThisThingIsTrue
                  AND ThisOtherThingIsTrue) THEN 0
                                            ELSE 1
       END AS UnrelatedConcept2,
FROM SomeTable

This code goes into a view, and the problem is that code using this view just wants to know the value of UnrelatedConcept2 - it would be confusing for the client code to derive UnrelatedConcept2 by specifying NOT Concept1. And it would be nice to not have to repeat the CASE WHEN statement when I could just NOT the value of Concept1.

Hopefully that makes sense.

Upvotes: 0

Views: 69

Answers (1)

YYamil
YYamil

Reputation: 1130

If I understood correctly you are trying to use a calculated value in the same query. If that's the question, someone answered it here: How to use calculated field in another field of the same query

Upvotes: 1

Related Questions