有一统计如何去除重复项!请教,谢谢!!
表名: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为楼房总层数
希望统计出如下形式(其中要求去除楼房编号重复项):
楼房编号 房屋类型 楼房总层数 可售数 待售数 已售数 预订数
[解决办法]
会有重复是因为同一幢楼的houseType或者Genelfloor有不同
[解决办法]
ID BuildID HouseCode HouseType SaleType GenelFloor
其中:ID主建,BuildID楼盘ID,HouseCode楼房编号比如3幢,
HouseType房屋类型:1,住宅;2,商用房
SaleType销售状态:1,可售;2,待售;3,已售;4,预订
GenelFloor为楼房总层数
楼房编号 房屋类型 楼房总层数 可售数 待售数 已售数 预订数
select main.HouseCode,main.HouseType,main.GenelFloor,a.qui,b.qui,c.qui,d.qui
from (select HouseCode,HouseType,GenelFloor) main
left join (select HouseCode,count(*) as qui from Tg_HouseInfo where SaleType=1 group by HouseCode) a on main.HouseCode=a.HouseCode
left join (select HouseCode,count(*) as qui from Tg_HouseInfo where SaleType=2 group by HouseCode) b on main.HouseCode=b.HouseCode
left join (select HouseCode,count(*) as qui from Tg_HouseInfo where SaleType=3 group by HouseCode) c on main.HouseCode=c.HouseCode
left join (select HouseCode,count(*) as qui from Tg_HouseInfo where SaleType=4 group by HouseCode) d on main.HouseCode=d.HouseCode