SQL Server 2012 - State-Based Query Columns

I am trying to query for a table based on a condition. For example let's say I have table A with columns c1, c2, c3, c4, and c5.

Pseudo code:

If c5='Y'
Then
    select c1, c2 from A
else
    select c3, c4 from A

I am looking for something like the following:

case when c5='Y' then select c1, c2 from A
else select c3, c4 from A

Any help would be appreciated.

Your case expression code doesn't make sense logically. You have to put the case expression on the row, not the table. Something like this.

select case when c5 = 'Y' then c1 else c3 end as MyFirstColumn
    , case when c5 = 'Y' then c2 else c4 end as MySecondColumn
from A
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章