在另一个变量中使用子查询的计算值 [英] Use calculated values from subquery in another variable

查看:32
本文介绍了在另一个变量中使用子查询的计算值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a complicated sub query that displays [Days Open]. I want to use this [Days Open] as a value in a another variable. Here is a simplified code example.

如果我将整个子选择粘贴到第二个子选择=[打开天数] 查询有效的地方,它只需要 3 分钟.

If I take the entire subselect and paste it wherever the second subselect=[days open] the query works it just takes 3 min.

我只希望第二个子查询引用第一个子查询的计算值

I just want the second subquery to reference the calculated value from the first subquery

     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

推荐答案

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

I would move the SubSelect down into the FROM clause, trating it like a virtual table. That way you can refence its value multiple times within the SELECT clause.

这篇关于在另一个变量中使用子查询的计算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆