1

I'm developping an asp.net project. I need to filter database with two different conditions and show each in a pie chart. So i need to get two columns in one query.

Column 1 :

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

Column 2 :

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

I searched many solutions, i tried UNION but result was this

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

i just need this.

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

2 回答 2

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)
于 2013-09-24T08:33:21.977 回答
0

虽然接受的答案会起作用,但这看起来像是PIVOT查询的主要情况。

select * 
from atmterminalfile 
pivot (count(id) for atlinestat in ([1],[2])) p
于 2013-09-24T09:01:48.673 回答