ankur
ankur

Reputation: 4733

add a computed column in sql query

I have a situation where I have two tables and I want to have a computed column.

Table 1: VacationMaster

PrefVacationId     |      Description
-----------------------------------------------
1                  |        abc
2                  |        xyz
3                  |        lmn
4                  |        pqr
5                  |        wqa

Table 2: VacationMapping

PrefVacationId [fk]    |      UseID  |   ID (PK of this table) 
-----------------------------------------------
    1                  |        1    |    1
    4                  |        1    |    2
    5                  |        1    |    3
    1                  |        2    |    4
    2                  |        3    |    5

What I need is every time all the list vacation available will be coming from master table for every user. I will be supplying userid as an input to my procedure and the result should look like this.

For userID = 1 as input

PrefVacationId     |      Description  |       IsSelected(Computed column)
-------------------------------------------------------------------
1                  |        abc        |        true        
2                  |        xyz        |        false
3                  |        lmn        |        false
4                  |        pqr        |        true
5                  |        wqa        |        true

Hope I am making some sense, how to make this query.

Upvotes: 0

Views: 247

Answers (1)

codingbiz
codingbiz

Reputation: 26386

You can try this

Select 
   vm.PrefVacationId,
   Description,
   CASE WHEN vMap.PrefVacationId IS NOT NULL THEN 'True'
   ELSE 'False'
   END IsSelected
From VacationMaster vm
Left Outer Join VacationMapping vMap
ON vm.PrefVacationId = vMap.PrefVacationId 
AND vMap.UserID = @UserID

Upvotes: 1

Related Questions