2013-09-24 78 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 
相關問題