日期:2014-05-18  浏览次数:20754 次

SQL 怎么连接两个 查询结果??????
with SumTable as (
SELECT Count(b.OptionItemsName) as SubmitSum ,b.OptionItemsName
FROM ArchivesReportInfo a inner JOIN OptionItems b
ON a.OptionTypeID = b.OptionItemsID
group by b.OptionItemsName
)

with AdoptTable as (
SELECT Count(b.OptionItemsName) as SubmitSum ,b.OptionItemsName
FROM ArchivesReportInfo a inner JOIN OptionItems b
ON a.OptionTypeID = b.OptionItemsID
where Examine = 1
group by b.OptionItemsName
)

select *
from SumTable a inner JOIN AdoptTable b
on a.OptionItemsName = b.OptionItemsName



像上面这么写报错了

如果此语句是公用表表达式、xmlnamespaces 子句或者更改跟踪上下文子句,那么前一个语句必须以分号结尾。
求达人

------解决方案--------------------
;with SumTable as (
SELECT Count(b.OptionItemsName) as SubmitSum ,b.OptionItemsName
FROM ArchivesReportInfo a inner JOIN OptionItems b
ON a.OptionTypeID = b.OptionItemsID
group by b.OptionItemsName
),
AdoptTable as (
SELECT Count(b.OptionItemsName) as SubmitSum ,b.OptionItemsName
FROM ArchivesReportInfo a inner JOIN OptionItems b
ON a.OptionTypeID = b.OptionItemsID
where Examine = 1
group by b.OptionItemsName
)

select *
from SumTable a inner JOIN AdoptTable b
on a.OptionItemsName = b.OptionItemsName

------解决方案--------------------
with SumTable as (
SELECT Count(b.OptionItemsName) as SubmitSum ,b.OptionItemsName
FROM ArchivesReportInfo a inner JOIN OptionItems b
ON a.OptionTypeID = b.OptionItemsID
group by b.OptionItemsName
),
 AdoptTable as (
SELECT Count(b.OptionItemsName) as SubmitSum ,b.OptionItemsName
FROM ArchivesReportInfo a inner JOIN OptionItems b
ON a.OptionTypeID = b.OptionItemsID
where Examine = 1
group by b.OptionItemsName
)

select *
from SumTable a inner JOIN AdoptTable b
on a.OptionItemsName = b.OptionItemsName