SQL Server - 如何使用派生表计算字段?

时间:2013-02-05 22:52:20

标签: sql sql-server sql-server-2008-r2

我正在尝试在查询中的WHERE子句中使用计算字段值,经过一些研究后我知道我需要创建一个派生表。但是我不确定语法,因为我的计算字段使用CASE语句,如:

        CASE T.IsReassigned
        WHEN 1 THEN DATEDIFF(MINUTE,
                    (SELECT top 1 SXAVWFTaskHistory.CreatedWhenUTC from SXAVWFTaskHistory where TaskID = T.TaskID and StatusID = 7 order by TaskHistoryID desc),
                    SYSDATETIMEOFFSET())                        
        ELSE 
        CASE Stat.StatusID      
            WHEN 1 THEN DATEDIFF(MINUTE,TSK.CreatedWhenUTC, SYSDATETIMEOFFSET())    -- time duration between when task is created(use SXAVWFTask.CreatedWhenUTC) and now 
            WHEN 2 THEN DATEDIFF(MINUTE,TSK.CreatedWhenUTC, SYSDATETIMEOFFSET())    -- time duration between when task is created(use SXAVWFTask.CreatedWhenUTC) and now
            ELSE DATEDIFF(MINUTE,TSK.CreatedWhenUTC, TH.CreatedWhenUTC) 
        END 
    END as TaskItemAge,

然后我想在where子句中使用TaskItemAge值,如:

WHERE TaskItemAge > @TaskAgeStart AND TaskItemAge < @TaskAgeEnd

如何将CASE语句放入子选择?

** * ** * ** * 的** * ** 编辑 ** * ** * ** * ** * 对不起,我对这怎么会有点困惑。这是我查询的一个更大的片段:

    SELECT TSK.TaskID, --0
    TH.IsLatest,
    TH.CreatedWhenUTC as TaskHistoryCreationDate, --10
    TS.Name AS STATE,
    CASE T.IsReassigned
        WHEN 1 THEN DATEDIFF(MINUTE,
                    (SELECT top 1 SXAVWFTaskHistory.CreatedWhenUTC from SXAVWFTaskHistory where TaskID = T.TaskID and StatusID = 7 order by TaskHistoryID desc),
                    SYSDATETIMEOFFSET())                        
        ELSE 
        CASE Stat.StatusID      
            WHEN 1 THEN DATEDIFF(MINUTE,TSK.CreatedWhenUTC, SYSDATETIMEOFFSET())    -- time duration between when task is created(use SXAVWFTask.CreatedWhenUTC) and now 
            WHEN 2 THEN DATEDIFF(MINUTE,TSK.CreatedWhenUTC, SYSDATETIMEOFFSET())    -- time duration between when task is created(use SXAVWFTask.CreatedWhenUTC) and now
            ELSE DATEDIFF(MINUTE,TSK.CreatedWhenUTC, TH.CreatedWhenUTC) 
        END 
    END as TaskItemAge,
    ctx.ContextTypeName,  
    ctx.ContextDescription, --15
    TH.TouchedWhenUTC as TaskHistoryModifiedDate
INTO #ii        
FROM SXAVWFTask TSK 
INNER JOIN SXAVWFTaskHistory TH ON TSK.TaskID = TH.TaskID
INNER JOIN @PagedTemp T on TH.TaskHistoryID = T.TaskHistoryID

Where TaskItemAge > @TaskAgeStart AND TaskItemAge < @TaskAgeEnd

3 个答案:

答案 0 :(得分:2)

select  *
from    (
        select  case ... end as CaseColumn
        ,       *
        from    YourTable
        ) as SubQueryAlias
where   CaseColumn between 1 and 2

答案 1 :(得分:1)

Andomar是正确的形式(+1),这是您的具体语法:

SELECT *
FROM (
    SELECT *,
        CASE T.IsReassigned
            WHEN 1 THEN DATEDIFF(MINUTE,
                        (SELECT top 1 SXAVWFTaskHistory.CreatedWhenUTC from SXAVWFTaskHistory where TaskID = T.TaskID and StatusID = 7 order by TaskHistoryID desc),
                        SYSDATETIMEOFFSET())                        
            ELSE 
            CASE Stat.StatusID      
                WHEN 1 THEN DATEDIFF(MINUTE,TSK.CreatedWhenUTC, SYSDATETIMEOFFSET())    -- time duration between when task is created(use SXAVWFTask.CreatedWhenUTC) and now 
                WHEN 2 THEN DATEDIFF(MINUTE,TSK.CreatedWhenUTC, SYSDATETIMEOFFSET())    -- time duration between when task is created(use SXAVWFTask.CreatedWhenUTC) and now
                ELSE DATEDIFF(MINUTE,TSK.CreatedWhenUTC, TH.CreatedWhenUTC) 
            END 
        END as TaskItemAge
    FROM BaseTable
) as DerivedTable
WHERE TaskItemAge > @TaskAgeStart AND TaskItemAge < @TaskAgeEnd

答案 2 :(得分:1)

您也可以使用CTE

  ;WITH cte AS
   (
    SELECT CASE WHEN ... THEN ... END AS TaskItemAge
    FROM your_table
    )
    SELECT *
    FROM cte
    WHERE TaskItemAge > @TaskAgeStart AND TaskItemAge < @TaskAgeEnd