读书人

一个统计有关问题升级了想了了一个上

发布时间: 2012-03-20 14:01:11 作者: rapoo

一个统计问题升级了,想了了一个上午都无法做出来,还是要请教了。。
一个统计问题升级了,想了了一个上午都无法做出来,还是要请教了。真的麻烦大家了。。解决了还是马上给分。
表1:TelClass---电话号段类型表
字段(Tname(varchar),Tvalue(varchar)) (注明:这里的数据是动态的,可能删,增,改)
数据如: (移动,135|138|139)
(联通,130|131|132)
(小灵通,02881|02889)
............
表2:TelNubmer---电话查询表
字段(ID(identity),TelNumber(varchar),Type(int))(注明:这里的Type其实是关联CType表的TID(

而且实质是子类的ID))
数据如:(1,13880056625,2)
(2,13035687567,3)
(3,13965588847,4)
(4,0288952555,6)
(5,13069369857,7)
(6,13054711369,9)
(7,0288158555,2)
(9,13599852214,4)
..............
表2:CTpye---门类表
字段(TID,T_Name,T_Father)
数据如:(1,陆地工具,0)
(2,汽车,1)
(3,自行车,1)
(4,火车,1)
(5,水上工具,0)
(6,轮船,5)
(7,气垫船,5)
(8,空域工具,0)
(9,飞机,8)

得到的统计如下(要求按照CType的大-〉小的顺序依次统计处各电话号段的电话数量):

门类 移动 联通 小灵通 合计
---------------------------------------------------
陆地工具 2 1 2 5
汽车 1 0 1 2
自行车 0 1 0 1
火车 1 0 1 2


水上工具 0 1 1 2
轮船 0 0 1 1
气垫船 0 1 0 1
空域工具 0 1 0 1
飞机 0 1 0 1



[解决办法]


declare @a table(tname varchar(20),Tvalue varchar(20))
insert @a
select '移动 ', '135|138|139 '
union all
select '联通 ', '130|131|132 '
union all
select '灵通 ', '02881|02889 '


declare @b table (id int identity(1,1),TelNumber varchar(20),type int)
insert @b
select '13880056625 ',2
union all
select '1303568756 ',3
union all
select '13965588847 ',4
union all
select '0288952555 ',6
union all
select '13069369857 ',7
union all
select '13054711369 ',9
union all
select '288158555 ',2
union all
select '13599852214 ',4


declare @c table(tid int identity(1,1),T_Name varchar(20),T_Father int)
insert @c
select '陆地工具 ',0
union all
select '汽车 ',1
union all
select '自行车 ',1
union all
select '火车 ',1
union all
select '水上工具 ',0
union all
select '轮船 ',5
union all
select '气垫船 ',5
union all
select '空域工具 ',0
union all
select '飞机 ',8

select *
from
(select * from @c c,
(select a.tname as tname,b.type as type,count(1) as num from @a a,@b b where charindex(left(b.TelNumber,3),a.Tvalue)> 0 or charindex(left(b.TelNumber,5),a.Tvalue)> 0

group by b.type,a.tname) d
where c.tid=d.type ) e, @c c

where e.T_Father=c.tid

没写完 下面的不知道写了 @!!

这是我写出来的结果为:
/*

tid T_Name T_Father tname type num tid T_Name T_Father
----------- -------------------- ----------- -------------------- ----------- ----------- ----------- -------------------- -----------
3 自行车 1 联通 3 1 1 陆地工具 0
7 气垫船 5 联通 7 1 5 水上工具 0
9 飞机 8 联通 9 1 8 空域工具 0


2 汽车 1 灵通 2 1 1 陆地工具 0
6 轮船 5 灵通 6 1 5 水上工具 0
2 汽车 1 移动 2 1 1 陆地工具 0
4 火车 1 移动 4 2 1 陆地工具 0

(所影响的行数为 7 行)
*/
希望高手修改下 如何显示

[解决办法]
不好意思写漏了 合计,下面的应该是你想要的:
create table #TelClass (Tname varchar(50),Tvalue varchar(200))
insert into #TelClass
select '移动 ', '135|138|139 ' union all
select '联通 ', '130|131|132 ' union all
select '小灵通 ', '02881|02889 '


create table #TelNubmer (ID int,TelNumber varchar(20),Type int)
insert into #TelNubmer
select 1, '13880056625 ',2 union all
select 2, '13035687567 ',3 union all
--select 3, '13965588847 ',4 union all
--select 4, '0288952555 ',6 union all
--select 5, '13069369857 ',7 union all
--select 6, '13054711369 ',9 union all
select 7, '0288158555 ',2 union all
select 9, '13599852214 ',4

create table #CTpye (TID int,T_Name varchar(50),T_Father int)
insert into #CTpye
select 1, '陆地工具 ',0 union all
select 2, '汽车 ',1 union all
select 3, '自行车 ',1 union all
select 4, '火车 ',1 union all
select 5, '水上工具 ',0 union all
select 6, '轮船 ',5 union all
select 7, '气垫船 ',5 union all
select 8, '空域工具 ',0 union all
select 9, '飞机 ',8

select [门类]=d.T_Name,b.Tname,d.TID,c.T_Name,TID2=c.TID
into #T1
from #TelNubmer a
left join #TelClass b
on charindex(left(a.TelNumber,3),b.Tvalue)> 0 or charindex(left(a.TelNumber,5),b.Tvalue)> 0
left join #CTpye c
on a.Type=c.TID
left join #CTpye d
on d.TID=c.T_Father

--select * from #T1
declare @strTemp varchar(500)
declare @strTemp1 varchar(500)
declare @sql varchar(1000)
set @sql= 'select [门类],TID=max(TID) '
select @sql=@sql+ ',[ '+Tname+ ']=sum(case Tname when ' ' '+Tname+ ' ' ' then 1 else 0 end) ' from #T1 group by Tname
set @sql=@sql+ ' into #T2 from #T1 group by [门类] '

set @sql=@sql+ ' select [门类]=T_Name,TID=max(TID2) '
select @sql=@sql+ ',[ '+Tname+ ']=sum(case Tname when ' ' '+Tname+ ' ' ' then 1 else 0 end) ' from #T1 group by Tname
set @sql=@sql+ ' into #T3 from #T1 group by T_Name '
set @strTemp= '[合计]=0 '
select @strTemp=@strTemp+ '+ '+Tname from #T1 group by Tname
set @strTemp1= ' '
select @strTemp1=@strTemp1+ '[ '+Tname+ ']=isnull(a. '+Tname+ ',0), ' from #T1 group by Tname
set @strTemp1=@strTemp1+ '合计=isnull(a.合计,0) '
exec(@sql+ ' select [门类]=b.T_Name, '+@strTemp1+ '
from (select *, '+@strTemp+ ' from #T2 union all
select *, '+@strTemp+ ' from #T3 ) a right join #CTpye b on a.TID=b.TID order by b.TID ')

读书人网 >SQL Server

热点推荐