日期:2014-05-19  浏览次数:20392 次

各位兄台帮我看看这个统计的子查询谢谢!
表名:Tg_HouseInfo
字段如下:
ID   BuildID   HouseCode   HouseType   SaleType   GenelFloor
1         1               4                     1                 1                     11
2         2               5                     1                 1                     7
3         3               1                     1                 2                     11
4         3               2                     2                 4                     6
5         4               4                     1                 1                     12
6         4               11                   1                 3                     6
...............................................
其中:ID主建,BuildID楼盘ID,HouseCode楼房编号比如3幢,
HouseType房屋类型:1,住宅;2,商用房
SaleType销售状态:1,可售;2,待售;3,已售;4,预订
GenelFloor为楼房总层数
希望统计出如下形式:
楼房编号   房屋类型     楼房总层数     可售数     待售数     已售数     预订数


------解决方案--------------------
select
HouseCode, HouseType, GenelFloor,
可售数 = sum(case SaleType when 1 then 1 else 0 end),
待售数 = sum(case SaleType when 2 then 1 else 0 end),
已售数 = sum(case SaleType when 3 then 1 else 0 end),
预订数 = sum(case SaleType when 4 then 1 else 0 end)
from
Tg_HouseInfo
group by
HouseCode, HouseType, GenelFloor