首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

各位兄台帮小弟我看看这个统计的子查询多谢

2012-01-29 
各位兄台帮我看看这个统计的子查询谢谢!表名:Tg_HouseInfo字段如下:IDBuildIDHouseCodeHouseTypeSaleTypeG

各位兄台帮我看看这个统计的子查询谢谢!
表名: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

热点排行