是否可以在 CTE 中编写 CTE?
Is it possible to write a CTE within a CTE?
我希望它遵循这个逻辑,但解释器不喜欢这段代码.
I want it to follow this logic, but the interpreter doesn't like this code.
with outertest as(
with test as (
select
SRnum,
gamenumber,
StartOfDistribution,
ApplicationNumber
from #main
where startofdistribution = '2011-06-14 00:00:00.000'
and SRnum = '313'
--order by SRnum, gamenumber, StartOfDistribution, ApplicationNumber
)
select
ApplicationNumber
,count(*) as RetailerAppearance
from test
group by ApplicationNumber
having count(*) = 4
) select count(*) from outertest
您不能在 SQL Server 中嵌套这样的 CTE,但您可以通过以下方式使用多个 CTE:
You can't nest CTEs like that in SQL Server but you can use multiple CTEs the following way:
;with test as
(
select
SRnum,
gamenumber,
StartOfDistribution,
ApplicationNumber
from #main
where startofdistribution = '2011-06-14 00:00:00.000'
and SRnum = '313'
--order by SRnum, gamenumber, StartOfDistribution, ApplicationNumber
),
outertest as
(
select
ApplicationNumber
,count(*) as RetailerAppearance
from test
group by ApplicationNumber
having count(*) = 4
)
select count(*)
from outertest
这篇关于CTE 中的 CTE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
截断时的 SQL 触发器SQL trigger on Truncate(截断时的 SQL 触发器)
具有多个可选搜索参数的 sql 搜索查询sql search query with multiple optional search parameters(具有多个可选搜索参数的 sql 搜索查询)
SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUPSQL Efficiency: WHERE IN Subquery vs. JOIN then GROUP(SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP)
使用 t-SQL 检索 XML 元素名称Retrieving XML element name using t-SQL(使用 t-SQL 检索 XML 元素名称)
在 SQL 输出中插入双引号Insert double quotes into SQL output(在 SQL 输出中插入双引号)
从 SQL SERVER 中的 CTE 中删除行Delete rows from CTE in SQL SERVER(从 SQL SERVER 中的 CTE 中删除行)