Suppose I have three tables like this:
TABLE_X TABLE_Y TABLE_Z
---------------------------------------
2012/1/1 a b
2012/1/2 a b
... a b
2012/3/17 a b
2012/3/18 a b
2012/3/19 a b
... a b
2012/12/22 a b
2012/12/23 a b
2012/12/24 a b
... a b
2013/1/1 a b
I need to do a view that uses GETDATE() as a condition. All values on and in the past of GETDATE() will take on TABLE_Y cols. All values in the future of GETDATE() will take on TABLE_Z cols.
view_TABLE_X_JOIN_Y_Z
---------------------
2012/1/1 a
2012/1/2 a
... a
2012/3/17 a
2012/3/18 a
2012/3/19 a
... a
2012/12/22 a
GETDATE() a
2012/12/24 b
... b
2013/1/1 b
2013/1/2 b
What I have tried:
SELECT x.Date
,CASE WHEN GETDATE() > x.Date THEN z.Value
ELSE y.Value
END AS Value
FROM TABLE_X x
LEFT JOIN TABLE_Y y ON x.Date = y.Date
LEFT JOIN TABLE_Z z ON x.Date = z.Date
The problem is that dates are not perfect FK references. i.e. Dates in TABLE_X is complete, but they are missing in some places in Y and Z.
You can use something like this: