3

我的 SQL 查询打印结果如

North   South   West    East    Central
0       280     0       41      36

但我想要它喜欢

North    0
South    280
West     0
East     41
Central  36

SQL:-

Select  Count(Case When Region=1 Then 1 Else Null End)[North],
    Count(Case When Region=2 Then 1 Else Null End)[South],
    Count(Case When Region=3 Then 1 Else Null End)[West],
    Count(Case When Region=4 Then 1 Else Null End)[East],
    Count(Case When Region=5 Then 1 Else Null End)[Central] 
    From ATM Where ATMStatus=0 And Bank=1
4

2 回答 2

3

使用分组

SELECT 
   CASE Region 
    WHEN 1 THEN 'North'
    WHEN 2 THEN 'South' 
    WHEN 3 THEN 'West'
    WHEN 4 THEN 'East'
    WHEN 5 THEN 'Central' END AS Region  
    , COUNT(ID)   --or your primary key if it is different
FROM ATM
WHERE ATMStatus = 0 AND Bank = 1
GROUP BY Region
于 2012-09-11T08:49:52.897 回答
1
 select case Region
     when 1 then 'North'
     when 2 then 'South'
     etc

 end, count(*)
 From ATM Where ATMStatus=0 And Bank=1  
 group by Region

有地区表吗?这将使它更简单,特别是如果 0 行很重要。

于 2012-09-11T08:50:02.197 回答