将一个表中的值视为另一个表中的列标题

时间:2016-04-20 04:45:20

标签: sql sql-server lookup

我有一个基表,我需要根据条目的类型计算两个日期之间的差异。

TBLA

+----------+------------+---------------+--------------+
| TypeCode |  Log_Date  | Complete_Date | Pending_Date |
+----------+------------+---------------+--------------+
|        1 | 18/04/2016 | 19/04/2016    |              |
|        2 | 10/04/2016 | 18/04/2016    | 15/04/2016   |
|        3 | 12/04/2016 | 19/04/2016    |              |
|        4 | 15/04/2016 | 17/04/2016    | 16/04/2016   |
|        5 | 16/04/2016 | 21/04/2016    |              |
|        1 | 19/04/2016 | 20/04/2016    |              |
|        2 | 20/03/2016 | 31/03/2015    |              |
|        3 | 25/03/2016 | 28/03/2016    |              |
|        4 | 26/03/2016 | 27/03/2016    |              |
|        5 | 27/03/2016 | 30/03/2016    |              |
+----------+------------+---------------+--------------+

我有另一个查找表,它具有基于TypeCode考虑的列名。

TBLB

+----------+----------+---------------+
| TypeCode | DateCol1 |   DateCol2    |
+----------+----------+---------------+
|        1 | Log_Date | Complete_Date |
|        2 | Log_Date | Pending_Date  |
|        3 | Log_Date | Complete_Date |
|        4 | Log_Date | Pending_Date  |
|        5 | Log_Date | Complete_Date |
+----------+----------+---------------+

我在计算两个日期之间做了一个简单的DATEDIFF。但是,我想从tblB查找要为此计算考虑的列,并根据tblA将其应用于TypeCode

结果表:

例如:当TypeCode24时,计算应为DATEDIFF(d, Log_Date, Pending_Date),否则为DATEDIFF(d, Log_Date, Complete_Date)

+----------+------------+---------------+--------------+----------+
| TypeCode |  Log_Date  | Complete_Date | Pending_Date | Cal_Days |
+----------+------------+---------------+--------------+----------+
|        1 | 18/04/2016 | 19/04/2016    |              |        1 |
|        2 | 10/04/2016 | 18/04/2016    | 15/04/2016   |        5 |
|        3 | 12/04/2016 | 19/04/2016    |              |        7 |
|        4 | 15/04/2016 | 17/04/2016    | 16/04/2016   |        1 |
|        5 | 16/04/2016 | 21/04/2016    |              |        5 |
|        1 | 19/04/2016 | 20/04/2016    |              |        1 |
|        2 | 20/03/2016 | 31/03/2015    |              |          |
|        3 | 25/03/2016 | 28/03/2016    |              |        3 |
|        4 | 26/03/2016 | 27/03/2016    |              |          |
|        5 | 27/03/2016 | 30/03/2016    |              |        3 |
+----------+------------+---------------+--------------+----------+

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

JOINCASE表达式一起使用:

SELECT
    a.*,
    Cal_Days =  
        DATEDIFF(
            DAY,
            CASE 
                WHEN b.DateCol1 = 'Log_Date' THEN a.Log_Date
                WHEN b.DateCol1 = 'Complete_Date' THEN a.Complete_Date
                ELSE a.Pending_Date
            END,
            CASE 
                WHEN b.DateCol2 = 'Log_Date' THEN a.Log_Date
                WHEN b.DateCol2 = 'Complete_Date' THEN a.Complete_Date
                ELSE a.Pending_Date
            END
        )
FROM TblA a
INNER JOIN TblB b
    ON b.TypeCode = a.TypeCode