我有一个带有通用表表达式的层次查询:
WITH Revision(REV, MonitoringGroupId, BusinessLineId, REVTYPE, REVEND) AS
(
SELECT REV, MonitoringGroupId, BusinessLineId, REVTYPE, REVEND
FROM MonitoringGroupBusinessLine_AUD
WHERE REV = 1045
UNION ALL
SELECT rev.REV, rev.MonitoringGroupId, rev.BusinessLineId, rev.REVTYPE, rev.REVEND
FROM MonitoringGroupBusinessLine_AUD rev
INNER JOIN Revision ON rev.REVEND = Revision.REV
)
SELECT
DISTINCT
MonitoringGroupId,
BusinessLineId,
IIF(REVTYPE = 2, 'REMOVE', 'ADD') as Operation
FROM Revision
我想将它用作子查询,诸如此类:
SELECT audit.MonitoringGroupId, audit.BusinessLineId, audit.Operation
FROM
(
WITH Revision(REV, MonitoringGroupId, BusinessLineId, REVTYPE, REVEND) AS
(
SELECT REV, MonitoringGroupId, BusinessLineId, REVTYPE, REVEND
FROM MonitoringGroupBusinessLine_AUD
WHERE REV = 1045
UNION ALL
SELECT rev.REV, rev.MonitoringGroupId, rev.BusinessLineId, rev.REVTYPE, rev.REVEND
FROM MonitoringGroupBusinessLine_AUD rev
INNER JOIN Revision ON rev.REVEND = Revision.REV
)
SELECT
DISTINCT
MonitoringGroupId,
BusinessLineId,
IIF(REVTYPE = 2, 'REMOVE', 'ADD') as Operation
FROM Revision
) audit
我有一个错误:
关键字“ with”附近的语法不正确。如果这个陈述是 公用表表达式,xmlnamespaces子句或更改跟踪 上下文子句,前一条语句必须以 分号。
我尝试在;
之前添加WITH
,但仍然无法正常工作。如何将我的分层查询用作子查询?
答案 0 :(得分:1)
您在输出中进行子查询。
;WITH Revision(REV, MonitoringGroupId, BusinessLineId, REVTYPE, REVEND) AS
(
SELECT REV, MonitoringGroupId, BusinessLineId, REVTYPE, REVEND
FROM MonitoringGroupBusinessLine_AUD
WHERE REV = 1045
UNION ALL
SELECT rev.REV, rev.MonitoringGroupId, rev.BusinessLineId, rev.REVTYPE, rev.REVEND
FROM MonitoringGroupBusinessLine_AUD rev
INNER JOIN Revision ON rev.REVEND = Revision.REV
)
SELECT audit.MonitoringGroupId, audit.BusinessLineId, audit.Operation
FROM
(
SELECT
DISTINCT
MonitoringGroupId,
BusinessLineId,
IIF(REVTYPE = 2, 'REMOVE', 'ADD') as Operation
FROM Revision
) audit