2013-09-24 81 views
1

我正在开发一个asp.net项目。我需要用两种不同的条件过滤数据库,并在饼图中显示每个条件。所以我需要在一个查询中获得两列。在一个查询中获取两个完全不同的列

1列:

select COUNT (*) 'OAS' from atmterminalfile (nolock) where Atstatu='2' and atlinestat ='1' 

2列:

select COUNT (*) 'OFS' from atmterminalfile (nolock) where Atstatu='2' and atlinestat ='2' 

我搜索了很多的解决方案,我想联盟,而且结果是这个

|  OAS  | 
|----------------| 
| Column 1 Count | 
| Column 2 Count | 

我只需要这一点。

|  OAS   |  OFS  | 
--------------------------------------- 
| Column 1 Count | Column 2 Count | 

回答

5
select sum(case when atlinestat = 1 then 1 else 0 end) 'OAS', 
     sum(case when atlinestat = 2 then 1 else 0 end) 'OFS' 
from atmterminalfile (nolock) 
where Atstatu='2' 
and atlinestat in (1,2) 
+0

工作,谢谢! –

+1

如何通过添加以下内容来加速:(1,2)中的atlinestat(atlinestat) –

0

虽然接受的答案将工作,这看起来像一个PIVOT查询主要情况。

select * 
from atmterminalfile 
pivot (count(id) for atlinestat in ([1],[2])) p 
相关问题