SQL Server - как использовать производную таблицу для вычисляемого поля?
Я пытаюсь использовать вычисленное значение поля в предложении 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,
Я хочу затем использовать значение TaskItemAge в предложении where что-то вроде:
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 ответа
select *
from (
select case ... end as CaseColumn
, *
from YourTable
) as SubQueryAlias
where CaseColumn between 1 and 2
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
Также вы можете использовать CTE
;WITH cte AS
(
SELECT CASE WHEN ... THEN ... END AS TaskItemAge
FROM your_table
)
SELECT *
FROM cte
WHERE TaskItemAge > @TaskAgeStart AND TaskItemAge < @TaskAgeEnd