7

I have 3 tables, lets say tables A, B, C to obfuscate my software :). A and B have two columns with numeric values and Table C has a Boolean column.

What I want is to create a view with a single column where depending on the column in C, either the value in A or B is selected.

Example:

Input:

        | A.val |    | B.val |    | C.val |
        ---------    ---------    ---------
entry1  |   1   |    |   6   |    |   T   |
entry2  |   2   |    |   8   |    |   F   |

Output:

       | D |
       -----
entry1 | 1 |
entry2 | 8 |

I'm wondering if there is a way to do this in a SQL statement(s) since I am currently doing it programmatically which eats up unnecessary resources.

Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
mascoj
  • 1,313
  • 14
  • 27

2 Answers2

7

If you're trying to select A if C = T or B if C = F then you can just use a Case Statement

Select (Case When C.val = TRUE Then A.val Else B.Val END) AS D
From Table
JamieD77
  • 13,796
  • 1
  • 17
  • 27
0

You can use CASE syntax for that.

SELECT CASE WHEN C.val = 'T' THEN A.val ELSE B.val END as yourField
Elon Than
  • 9,603
  • 4
  • 27
  • 37