问题
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.
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
回答1:
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.
来源:https://stackoverflow.com/questions/16487587/use-calculated-values-from-subquery-in-another-variable