0

我有一个显示 [Days Open] 的复杂子查询。我想将此 [Days Open] 用作另一个变量中的值。这是一个简化的代码示例。

如果我将整个子选择粘贴到第二个子选择 = [打开天数] 的任何位置,则查询只需 3 分钟。

我只想让第二个子查询引用第一个子查询的计算值

     SELECT 
Sub.Category AS 'Category',
    SubSelect(not actually value just shortening code) As [Days Open],

(Case 
When Sub.Category Like '%Part%' Then 
    (Case 
    When Cast([Days Open] As Int)>60 Then 'Late' 
    When Cast([Days Open] As Int)<61 Then 'Not Late'
    Else 'N/A' End)
When Sub.Category Like '%Determination%' Then 
    (Case 
    When Cast([Days Open] As Int)>1 Then 'Late' 
    When Cast([Days Open] As Int)<2 Then 'Not Late'
    Else 'N/A' End)
When Sub.Category Like '%History%' Then 
    (Case 
    When Cast([Days Open] As Int)>5 Then 'Late' 
    When Cast([Days Open] As Int)<6 Then 'Not Late'
    Else 'N/A' End)
When Sub.Category Like '%Eval%' Then 
    (Case 
    When Cast([Days Open] As Int)>45 Then 'Late' 
    When Cast([Days Open] As Int)<46 Then 'Not Late'
    Else 'N/A' End)
Else 'N/A' End) As 'Days Open Late'*/

    FROM Tasks As Sub
    WHERE (Sub.Status=2) And ((Sub.DateClosed Between '04/01/2013' And '04/30/2013 11:59:59 PM')) 
    Order By Sub.Category Asc, 'Days Open' Desc
4

1 回答 1

0

我会将 SubSelect 向下移动到FROM子句中,将其视为虚拟表。SELECT这样你就可以在子句中多次引用它的值。

于 2013-05-10T17:23:00.533 回答