高手帮忙,数据统计问题

powbcom 2006-11-01 04:20:44
表数据字段A,B,C,D 四个字段
每个字段存储的值为2、1、0、-1、2中其中一个,要求统计每个数字在A,B,C,D中的数量,
结果如下:
A B C D
8 6 8 8
12 5 6 54
54 23 23 34
4 7 78 45
12 45 47 78

先谢谢了
...全文
248 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2006-11-01
  • 打赏
  • 举报
回复
create table roy(A int,B int,C int,D int)
insert into roy select 1, 2, 0,-1
insert into roy select -2, 1,-1, 2
insert into roy select 1,-2, 2, 1
insert into roy select 0, 2, 0, 0
insert into roy select 2,-1,-2,-2
insert into roy select -1, 0, 1,-1

楼主结贴了,那我也回复一个动态的写法
declare @sql varchar(1000)
set @sql=''
select @sql=@sql+',['+cast(name as varchar)+']=sum(case '+name +' when b.id then 1 else 0 end)'
from syscolumns where id=object_id('roy')
set @sql= 'select b.id '+@sql+
' from roy,(select 2 as id union select 1 union select 0 union select -1 union select -2) b group by b.id'
exec(@sql)
id A B C D
----------- ----------- ----------- ----------- -----------
-2 1 1 1 1
-1 1 1 1 2
0 1 1 2 1
1 2 1 1 1
2 1 2 1 1
dawugui 2006-11-01
  • 打赏
  • 举报
回复
declare @t table(a int , b int , c int , d int)
insert @t
select 0,0,0,0 union all
select 1,1,1,1 union all
select 2,2,2,2 union all
select -1,-1,-1,-1 union all
select -1,-1,-1,-1 union all
select -2,-2,-2,-2

declare @tb table(id int)
insert @tb
select 0 union all
select 1 union all
select 2 union all
select -1 union all
select -2

select
b.id,
sum(case A when b.id then 1 else 0 end) as A,
sum(case B when b.id then 1 else 0 end) as B,
sum(case C when b.id then 1 else 0 end) as C,
sum(case D when b.id then 1 else 0 end) as D
from @t ,
(select * from @tb) b
group by b.id



id A B C D
----------- ----------- ----------- ----------- -----------
-2 1 1 1 1
-1 2 2 2 2
0 1 1 1 1
1 1 1 1 1
2 1 1 1 1

(所影响的行数为 5 行)
dawugui 2006-11-01
  • 打赏
  • 举报
回复
上面错了.

declare @t table(a int , b int , c int , d int)
insert @t
select 0,0,0,0 union all
select 1,1,1,1 union all
select 2,2,2,2 union all
select -1,-1,-1,-1 union all
select -1,-1,-1,-1 union all
select -2,-2,-2,-2

declare @tb table(id int)
insert @tb
select 0 union all
select 1 union all
select 2 union all
select -1 union all
select -2

select
b.id,
sum(case A when b.id then 1 else 0 end) as A,
sum(case B when b.id then 1 else 0 end) as B,
sum(case C when b.id then 1 else 0 end) as C,
sum(case D when b.id then 1 else 0 end) as D
from @t ,
(select 2 as id union select 1 union select 0 union select -1 union select -2) b
group by b.id



id A B C D
----------- ----------- ----------- ----------- -----------
-2 1 1 1 1
-1 2 2 2 2
0 1 1 1 1
1 1 1 1 1
2 1 1 1 1

(所影响的行数为 5 行)

dawugui 2006-11-01
  • 打赏
  • 举报
回复
declare @t table(a int , b int , c int , d int)
insert @t
select 0,0,0,0 union all
select 1,1,1,1 union all
select 2,2,2,2 union all
select -1,-1,-1,-1 union all
select -1,-1,-1,-1 union all
select -2,-2,-2,-2

declare @tb table(id int)
insert @tb
select 0 union all
select 1 union all
select 2 union all
select -1 union all
select -2

select
b.id,
sum(case A when 0 then 1 else 0 end) as A,
sum(case B when 1 then 1 else 0 end) as B,
sum(case C when 2 then 1 else 0 end) as C,
sum(case D when -1 then 1 else 0 end) as D
from @t ,
(select 2 as id union select 1 union select 0 union select -1 union select -2) b
group by b.id



id A B C D
----------- ----------- ----------- ----------- -----------
-2 1 1 1 2
-1 1 1 1 2
0 1 1 1 2
1 1 1 1 2
2 1 1 1 2

(所影响的行数为 5 行)
子陌红尘 2006-11-01
  • 打赏
  • 举报
回复
declare @t table(A int,B int,C int,D int)
insert into @t select 1, 2, 0,-1
insert into @t select -2, 1,-1, 2
insert into @t select 1,-2, 2, 1
insert into @t select 0, 2, 0, 0
insert into @t select 2,-1,-2,-2
insert into @t select -1, 0, 1,-1

select
b.id,
sum(case A when b.id then 1 else 0 end) as A,
sum(case B when b.id then 1 else 0 end) as B,
sum(case C when b.id then 1 else 0 end) as C,
sum(case D when b.id then 1 else 0 end) as D
from
@t a,
(select 2 as id union select 1 union select 0 union select -1 union select -2) b
group by
b.id

/*
id A B C D
----------- ----------- ----------- ----------- -----------
-2 1 1 1 1
-1 1 1 1 2
0 1 1 2 1
1 2 1 1 1
2 1 2 1 1
*/
中国风 2006-11-01
  • 打赏
  • 举报
回复
楼主是要统计ABCD列有以下数据的列?
2、1、0、-1、2
子陌红尘 2006-11-01
  • 打赏
  • 举报
回复
select
b.id,
sum(case A when b.id then 1 else 0 end) as A,
sum(case B when b.id then 1 else 0 end) as B,
sum(case C when b.id then 1 else 0 end) as C,
sum(case D when b.id then 1 else 0 end) as D
from
表 a,
(select 2 as id union select 1 union select 0 union select -1 union select -2) b
group by
b.id

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧