日期:2014-05-18  浏览次数:20462 次

100分请教一个简单的统计问题.搞定就结.
数据如下:
FuncID             C                   IDStr                                                  
-----------   ---------   --------------------  
101                   1                   1101000000
101                   1                   1100000000
101                   1                   1110000000
101                   1                   1100000000
104                   0                   0110000000
104                   1                   1110000000
104                   0                   0110000000
301                   0                   1101000000
301                   0                   0000100000

如何写一个统计的SQL语句,按照FuncID统计.
统计时:
如果C中有一个是1,则该值为1
IDStr如果该位有一个1则该位值为1
比如部分结果如下:
--------------------------------
101                   1                   1111000000
104                   1                   1110000000
.....
301                   0                   1101100000


C是bit型
IDStr是Varchar型

------解决方案--------------------
create table t2(funcid int,c int,idstr bigint)
insert into t2
select 101,1,1101000000
union all select 101,1,1100000000
union all select 101,1,1110000000
union all select 101,1,1100000000
union all select 104,0,0110000000
union all select 104,1,1110000000
union all select 104,0,0110000000
union all select 301,0,1101000000
union all select 301,0,0000100000

create function f_t2(@idstr bigint)
returns bigint
as
begin
declare @s varchar(10)
select @s=rtrim(@idstr)
return case when substring(@s,1,1)> '0 ' then '1 ' else '0 ' end+
case when substring(@s,2,1)> '0 ' then '1 ' else '0 ' end+
case when substring(@s,3,1)> '0 ' then '1 ' else '0 ' end+
case when substring(@s,4,1)> '0 ' then '1 ' else '0 ' end+
case when substring(@s,5,1)> '0 ' then '1 ' else '0 ' end+
case when substring(@s,6,1)> '0 ' then '1 ' else '0 ' end+
case when substring(@s,7,1)> '0 ' then '1 ' else '0 '