0

我想从内部子查询中选择列TeamProjectProjectNodeName& 。TestPlanName内部查询在从视图中获取结果时运行得非常好。但是当我如下运行整个查询时,出现以下错误 -

')' 附近的语法不正确。

请帮助我。

这是我的查询-

select 
    x.TeamProjectProjectNodeName, x.TestPlanName
from 
    (select 
         TeamProjectProjectNodeName, TestPlanName,ResultOutcome, count(ResultOutcome) 
     from [Tfs_Warehouse].[dbo].[TestResultView] 
     where TestPlanName <> 'NULL'
     GROUP BY TeamProjectProjectNodeName, TestPlanName, ResultOutcome
     order by TeamProjectProjectNodeName asc, TestPlanName asc) x

谢谢。

4

3 回答 3

4

将 order by 带出子查询,并将 count 列命名如下:

select 
   x.TeamProjectProjectNodeName,
   x.TestPlanName,
   x.Total
from (select 
         TeamProjectProjectNodeName,
         TestPlanName,
         ResultOutcome, 
         count(*) as Total 
      from [Tfs_Warehouse].[dbo].[TestResultView] 
      where TestPlanName IS NOT NULL
      GROUP BY TeamProjectProjectNodeName, TestPlanName, ResultOutcome
     ) x
order by TeamProjectProjectNodeName asc, TestPlanName asc 

我还将您的WHERE语法更改IS NOT NULL为@bluefeet 建议的那样,因为我想这是您实际需要的。

于 2013-06-04T15:06:37.597 回答
1

干得好 :

select x.TeamProjectProjectNodeName,x.TestPlanName
from (select top 100 percent TeamProjectProjectNodeName,TestPlanName,ResultOutcome, count(ResultOutcome) SomeAlias from [Tfs_Warehouse].[dbo].[TestResultView] 
where TestPlanName <> 'NULL'
GROUP BY TeamProjectProjectNodeName, TestPlanName, ResultOutcome
order by TeamProjectProjectNodeName asc, TestPlanName asc) x

除非您指定 top 并且 count() 应该有别名,否则您不能在内部选择中进行排序。

于 2013-06-04T15:06:53.710 回答
0

我建议给你count()一个别名和group by聚合函数以避免错误

select x.TeamProjectProjectNodeName,x.TestPlanName
from (select TeamProjectProjectNodeName,TestPlanName,ResultOutcome, count(ResultOutcome) as total from [Tfs_Warehouse].[dbo].[TestResultView] 
where TestPlanName <> 'NULL'
GROUP BY TeamProjectProjectNodeName, TestPlanName, ResultOutcome) x
于 2013-06-04T15:07:40.797 回答