过滤别名列
Filtering an aliased column
我在 MS-Access 中有以下查询,运行正常:
SELECT A1.LRN, A1.StartDate, A1.Destination,
(
SELECT TOP 1 A2.StartDate
FROM testEnrolment As A2
WHERE A2.LRN = A1.LRN AND A2.StartDate > A1.StartDate
ORDER BY A2.StartDate
) As NextStartDate,
(
SELECT TOP 1 B2.Destination
FROM testEnrolment As B2
WHERE B2.LRN = A1.LRN AND B2.StartDate > A1.StartDate
ORDER BY B2.StartDate
) As NextDestination
FROM testEnrolment As A1
2 个别名列 NextStartDate
、NextDestination
从当前 LRN
的下一条记录的 StartDate
和 Destination
字段获取数据。
所以如果 table testEnrolment 有这个数据:
LRN StartDate Destination
--------------------------------
L0001 01/08/2014 Unemployed
L0001 02/08/2014 Education
L0001 03/08/2014 Unemployed
L0002 20/09/2014 Education
L0002 21/09/2014
查询结果如下:
LRN StartDate Destination NextStartDate NextDestination
--------------------------------------------------------------
L0001 01/08/2014 Unemployed 02/08/2014 Education
L0001 02/08/2014 Education 03/08/2014 Unemployed
L0001 03/08/2014 Unemployed
L0002 20/09/2014 Education 21/09/2014
L0002 21/09/2014
我接下来要做的是通过 不等于 "Education".
的记录过滤列别名 NextDestination
WHERE
子句对列别名不起作用,我似乎也无法使 HAVING
起作用。
将您的 sql 包装到子查询中,以便您可以过滤别名
SELECT * FROM (
SELECT A1.LRN, A1.StartDate, A1.Destination,
(
SELECT TOP 1 A2.StartDate
FROM testEnrolment As A2
WHERE A2.LRN = A1.LRN AND A2.StartDate > A1.StartDate
ORDER BY A2.StartDate
) As NextStartDate,
(
SELECT TOP 1 B2.Destination
FROM testEnrolment As B2
WHERE B2.LRN = A1.LRN AND B2.StartDate > A1.StartDate
ORDER BY B2.StartDate
) As NextDestination
FROM testEnrolment As A1
) AS s
WHERE NextDestination <> 'Education'
我在 MS-Access 中有以下查询,运行正常:
SELECT A1.LRN, A1.StartDate, A1.Destination,
(
SELECT TOP 1 A2.StartDate
FROM testEnrolment As A2
WHERE A2.LRN = A1.LRN AND A2.StartDate > A1.StartDate
ORDER BY A2.StartDate
) As NextStartDate,
(
SELECT TOP 1 B2.Destination
FROM testEnrolment As B2
WHERE B2.LRN = A1.LRN AND B2.StartDate > A1.StartDate
ORDER BY B2.StartDate
) As NextDestination
FROM testEnrolment As A1
2 个别名列 NextStartDate
、NextDestination
从当前 LRN
的下一条记录的 StartDate
和 Destination
字段获取数据。
所以如果 table testEnrolment 有这个数据:
LRN StartDate Destination
--------------------------------
L0001 01/08/2014 Unemployed
L0001 02/08/2014 Education
L0001 03/08/2014 Unemployed
L0002 20/09/2014 Education
L0002 21/09/2014
查询结果如下:
LRN StartDate Destination NextStartDate NextDestination
--------------------------------------------------------------
L0001 01/08/2014 Unemployed 02/08/2014 Education
L0001 02/08/2014 Education 03/08/2014 Unemployed
L0001 03/08/2014 Unemployed
L0002 20/09/2014 Education 21/09/2014
L0002 21/09/2014
我接下来要做的是通过 不等于 "Education".
的记录过滤列别名NextDestination
WHERE
子句对列别名不起作用,我似乎也无法使 HAVING
起作用。
将您的 sql 包装到子查询中,以便您可以过滤别名
SELECT * FROM (
SELECT A1.LRN, A1.StartDate, A1.Destination,
(
SELECT TOP 1 A2.StartDate
FROM testEnrolment As A2
WHERE A2.LRN = A1.LRN AND A2.StartDate > A1.StartDate
ORDER BY A2.StartDate
) As NextStartDate,
(
SELECT TOP 1 B2.Destination
FROM testEnrolment As B2
WHERE B2.LRN = A1.LRN AND B2.StartDate > A1.StartDate
ORDER BY B2.StartDate
) As NextDestination
FROM testEnrolment As A1
) AS s
WHERE NextDestination <> 'Education'