0

我有一个看起来像这样的查询:

SELECT
    col1
    ,...
    ,col3
    ,(SELECT col3 FROM table where <clause>) AS MinPickTime
    ,(SELECT col3 FROM table where <clause>) AS MaxPickTime
    ,DATEDIFF(d, MinPickTime, MaxPickTime)
FROM table

但是 DATEDIFF 行不喜欢别名列。

简而言之,如何给 DATEDIFF 一个由子查询派生的别名列?

4

2 回答 2

1

使用派生表概念访问别名。

SELECT  col1
    ,...
    ,col3,
     MinPickTime,
     MaxPickTime ,
DATEDIFF(d, MinPickTime, MaxPickTime)
FROM (
SELECT
    col1
    ,...
    ,col3
    ,(SELECT col3 FROM table where <clause>) AS MinPickTime
    ,(SELECT col3 FROM table where <clause>) AS MaxPickTime

FROM table
)z
于 2013-10-07T03:48:12.967 回答
0
with temp as (SELECT
    col1
    ,...
    ,col3
    ,(SELECT col3 FROM table where <clause>) AS MinPickTime
    ,(SELECT col3 FROM table where <clause>) AS MaxPickTime
FROM table)

select DATEDIFF(d,MaxPickTime,MinPickTime) from temp
于 2013-10-07T03:55:07.703 回答