在select语句中使用WITH Query(CTE)

时间:2013-10-22 03:03:50

标签: sql sql-server tsql common-table-expression

以下CTE查询运作良好:

    ;WITH QResult AS 
(SELECT 
    ROW_NUMBER() OVER (ORDER BY Ad_Date DESC) AS Row,
    * 
 FROM [vw_ads]
) 
SELECT 
    Co1l, Col2, 
    MAX(Row) OVER()
FROM QResult

我需要的是select top(10)来自此查询,我尝试了以下

Select top (10) from With ...

但它引发了以下异常:

  

关键字'with'附近的语法不正确。如果这个陈述是   公用表表达式,xmlnamespaces子句或更改跟踪   context子句,前一个语句必须以a结尾   分号。

如何从WITH Query中选择聚合函数?

1 个答案:

答案 0 :(得分:2)

没试过..

;WITH QResult AS  (
SELECT ROW_NUMBER() OVER (ORDER BY Ad_Date DESC) AS Row, * FROM [vw_ads] ) 
SELECT top(10)
Co1l, Col2, 
MAX(Row) OVER() FROM QResult