2013-10-23 76 views
0

我有表如下所示:SQL - 基于多集团 - 列

A_ID   Area_Code  Status 
1    AAA    2   
2    BBB    1   
3    AAA    2   
4    AAA    3   
5    AAA    2   
6    BBB    2   
7    BBB    1   
8    AAA    4   
9    AAA    5   
10    AAA    4   

,我想结果集,如:

A_CodeCount_A_CodeCount_Sts1Count_Sts2Count_Sts3Count_Sts4Count_S TS5

AAA   7   0   3   1  2   1 

BBB   3   2   1   0  0   0 

请注意,我只有5雕像。所以我需要的雕像是列..

我感谢所有帮助。感谢

+1

那你试试? – mucio

+0

我不熟悉SQL。 – MohammadSia

回答

0
select area_code as a_code, 
     count(*) as count_a_code, 
     sum(case when status = 1 then 1 else 0 end) as code_count_sts1, 
     sum(case when status = 2 then 1 else 0 end) as code_count_sts2, 
     sum(case when status = 3 then 1 else 0 end) as code_count_sts3, 
     sum(case when status = 4 then 1 else 0 end) as code_count_sts4, 
     sum(case when status = 5 then 1 else 0 end) as code_count_sts5 
from your_table 
group by area_code