id type object money
1 0 AAA 100
2 2 AAA 100
3 3 BBB 100
4 4 BBB 100
5 5 CCC 100
6 3 CCC 100
7 4 DDD 100
8 0 EEE 100
想转换成这种格式
(type 0,2,3,4,5 分别为 B,D,E,F,G)
////////////////////////
type AAA BBB CCC DDD
B sum(money)
count()
D sum(money)
count()
E sum(money)
count()
F sum(money)
count()
G sum(money)
count()
谢谢!
13 个解决方案
#1
type AAA BBB CCC DDD
B sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
D sum(money) sum(money) sum(money) sum(money)
count() count() count() count()E
F sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
G sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
B sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
D sum(money) sum(money) sum(money) sum(money)
count() count() count() count()E
F sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
G sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
#2
create table a(id int,type int,object varchar(10),[money] int)
insert into a
select 1, 0 ,'AAA', 100 union all
select 2, 2 ,'AAA', 100 union all
select 3, 3 ,'BBB', 100 union all
select 4, 4 ,'BBB', 100 union all
select 5, 5 ,'CCC', 100 union all
select 6, 3 ,'CCC', 100 union all
select 7, 4 ,'DDD', 100 union all
select 8, 0 ,'EEE', 100
go
declare @sql1 varchar(8000),@sql2 varchar(8000)
set @sql1=''
select @sql1=@sql1+',sum(case object when '''+object+''' then [money] else 0 end) ['+object+']'
from a group by object
set @sql1='select ''sum'' sort,case type when 0 then ''B'' when 2 then ''D'' when 3 then ''E'' when 4 then ''F'' when 5 then ''G'' end type'
+@sql1+' from a group by type'
set @sql2=''
select @sql2=@sql2+',sum(case object when '''+object+''' then 1 else 0 end) ['+object+']'
from a group by object
set @sql2='select ''count'' sort,case type when 0 then ''B'' when 2 then ''D'' when 3 then ''E'' when 4 then ''F'' when 5 then ''G'' end type'
+@sql2+' from a group by type'
exec(@sql1+' union all '+@sql2+' order by type,sort desc')
drop table a
insert into a
select 1, 0 ,'AAA', 100 union all
select 2, 2 ,'AAA', 100 union all
select 3, 3 ,'BBB', 100 union all
select 4, 4 ,'BBB', 100 union all
select 5, 5 ,'CCC', 100 union all
select 6, 3 ,'CCC', 100 union all
select 7, 4 ,'DDD', 100 union all
select 8, 0 ,'EEE', 100
go
declare @sql1 varchar(8000),@sql2 varchar(8000)
set @sql1=''
select @sql1=@sql1+',sum(case object when '''+object+''' then [money] else 0 end) ['+object+']'
from a group by object
set @sql1='select ''sum'' sort,case type when 0 then ''B'' when 2 then ''D'' when 3 then ''E'' when 4 then ''F'' when 5 then ''G'' end type'
+@sql1+' from a group by type'
set @sql2=''
select @sql2=@sql2+',sum(case object when '''+object+''' then 1 else 0 end) ['+object+']'
from a group by object
set @sql2='select ''count'' sort,case type when 0 then ''B'' when 2 then ''D'' when 3 then ''E'' when 4 then ''F'' when 5 then ''G'' end type'
+@sql2+' from a group by type'
exec(@sql1+' union all '+@sql2+' order by type,sort desc')
drop table a
#3
--测试数据
create table tb(id int,type int,object varchar(10),[money] int)
insert tb select 1,0,'AAA',100
union all select 2,2,'AAA',100
union all select 3,3,'BBB',100
union all select 4,4,'BBB',100
union all select 5,5,'CCC',100
union all select 6,3,'CCC',100
union all select 7,4,'DDD',100
union all select 8,0,'EEE',100
go
--统计处理
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then [money] else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)'+@s+'
from tb group by type')
go
--删除测试
drop table tb
/*--测试结果]
type AAA BBB CCC DDD EEE
---- ----------- ----------- ----------- ----------- -----------
B 100 0 0 0 100
D 100 0 0 0 0
E 0 100 100 0 0
F 0 100 0 100 0
G 0 0 100 0 0
--*/
create table tb(id int,type int,object varchar(10),[money] int)
insert tb select 1,0,'AAA',100
union all select 2,2,'AAA',100
union all select 3,3,'BBB',100
union all select 4,4,'BBB',100
union all select 5,5,'CCC',100
union all select 6,3,'CCC',100
union all select 7,4,'DDD',100
union all select 8,0,'EEE',100
go
--统计处理
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then [money] else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)'+@s+'
from tb group by type')
go
--删除测试
drop table tb
/*--测试结果]
type AAA BBB CCC DDD EEE
---- ----------- ----------- ----------- ----------- -----------
B 100 0 0 0 100
D 100 0 0 0 0
E 0 100 100 0 0
F 0 100 0 100 0
G 0 0 100 0 0
--*/
#4
select max(case when type=0 then 'A' when type=2 then 'B' when type=3 then 'C' end),sum(case when object='aaa' then money end),sum(case when object='aaa' then 1 else 0 end),
sum(case when object='bbb' then money end),sum(case when object='bbb' then 1 else 0 end),
sum(case when object='ccc' then money end),sum(case when object='ccc' then 1 else 0 end),
sum(case when object='ddd' then money end),sum(case when object='ddd' then 1 else 0 end) from A group by type
sum(case when object='bbb' then money end),sum(case when object='bbb' then 1 else 0 end),
sum(case when object='ccc' then money end),sum(case when object='ccc' then 1 else 0 end),
sum(case when object='ddd' then money end),sum(case when object='ddd' then 1 else 0 end) from A group by type
#5
具体要显示什么,自己改改就行了
#6
能不能同时显示 count() 和 sum()?
谢谢大家先!
谢谢大家先!
#7
这样的格式:
type AAA BBB CCC DDD
B sum(money) sum(money) sum(money) sum(money)
B count() count() count() count()
D sum(money) sum(money) sum(money) sum(money)
D count() count() count() count()E
F sum(money) sum(money) sum(money) sum(money)
F count() count() count() count()
G sum(money) sum(money) sum(money) sum(money)
G count() count() count() count()
type AAA BBB CCC DDD
B sum(money) sum(money) sum(money) sum(money)
B count() count() count() count()
D sum(money) sum(money) sum(money) sum(money)
D count() count() count() count()E
F sum(money) sum(money) sum(money) sum(money)
F count() count() count() count()
G sum(money) sum(money) sum(money) sum(money)
G count() count() count() count()
#8
2楼就是啊,运行一下就知道了
#9
--没看到count的要求,稍改一下就行了.
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then case when b.id=1 then [money] else 1 end else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)'+@s+'
from tb,(select id=1 union all select 2)b group by b.id,type')
go
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then case when b.id=1 then [money] else 1 end else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)'+@s+'
from tb,(select id=1 union all select 2)b group by b.id,type')
go
#10
--测试数据
create table tb(id int,type int,object varchar(10),[money] int)
insert tb select 1,0,'AAA',100
union all select 2,2,'AAA',100
union all select 3,3,'BBB',100
union all select 4,4,'BBB',100
union all select 5,5,'CCC',100
union all select 6,3,'CCC',100
union all select 7,4,'DDD',100
union all select 8,0,'EEE',100
go
--统计处理
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then case when b.id=1 then [money] else 1 end else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)+b.name'+@s+'
from tb,(select id=1,name=''_sum'' union all select 2,''_count'')b group by type,b.id,b.name')
go
--删除测试
drop table tb
/*--测试结果]
type AAA BBB CCC DDD EEE
-------- ----------- ----------- ----------- ----------- -----------
B_sum 100 0 0 0 100
B_count 1 0 0 0 1
D_sum 100 0 0 0 0
D_count 1 0 0 0 0
E_sum 0 100 100 0 0
E_count 0 1 1 0 0
F_sum 0 100 0 100 0
F_count 0 1 0 1 0
G_sum 0 0 100 0 0
G_count 0 0 1 0 0
--*/
#11
问个巨菜的问题,,,单位器材器材资料是.dbf的,我在sql server2000里怎么导入这个这些表?望高手不吝解答,步骤给的清晰明了点,谢谢了
#12
收藏之
#13
怎么动态语句老是不懂啊,郁闷
#1
type AAA BBB CCC DDD
B sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
D sum(money) sum(money) sum(money) sum(money)
count() count() count() count()E
F sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
G sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
B sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
D sum(money) sum(money) sum(money) sum(money)
count() count() count() count()E
F sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
G sum(money) sum(money) sum(money) sum(money)
count() count() count() count()
#2
create table a(id int,type int,object varchar(10),[money] int)
insert into a
select 1, 0 ,'AAA', 100 union all
select 2, 2 ,'AAA', 100 union all
select 3, 3 ,'BBB', 100 union all
select 4, 4 ,'BBB', 100 union all
select 5, 5 ,'CCC', 100 union all
select 6, 3 ,'CCC', 100 union all
select 7, 4 ,'DDD', 100 union all
select 8, 0 ,'EEE', 100
go
declare @sql1 varchar(8000),@sql2 varchar(8000)
set @sql1=''
select @sql1=@sql1+',sum(case object when '''+object+''' then [money] else 0 end) ['+object+']'
from a group by object
set @sql1='select ''sum'' sort,case type when 0 then ''B'' when 2 then ''D'' when 3 then ''E'' when 4 then ''F'' when 5 then ''G'' end type'
+@sql1+' from a group by type'
set @sql2=''
select @sql2=@sql2+',sum(case object when '''+object+''' then 1 else 0 end) ['+object+']'
from a group by object
set @sql2='select ''count'' sort,case type when 0 then ''B'' when 2 then ''D'' when 3 then ''E'' when 4 then ''F'' when 5 then ''G'' end type'
+@sql2+' from a group by type'
exec(@sql1+' union all '+@sql2+' order by type,sort desc')
drop table a
insert into a
select 1, 0 ,'AAA', 100 union all
select 2, 2 ,'AAA', 100 union all
select 3, 3 ,'BBB', 100 union all
select 4, 4 ,'BBB', 100 union all
select 5, 5 ,'CCC', 100 union all
select 6, 3 ,'CCC', 100 union all
select 7, 4 ,'DDD', 100 union all
select 8, 0 ,'EEE', 100
go
declare @sql1 varchar(8000),@sql2 varchar(8000)
set @sql1=''
select @sql1=@sql1+',sum(case object when '''+object+''' then [money] else 0 end) ['+object+']'
from a group by object
set @sql1='select ''sum'' sort,case type when 0 then ''B'' when 2 then ''D'' when 3 then ''E'' when 4 then ''F'' when 5 then ''G'' end type'
+@sql1+' from a group by type'
set @sql2=''
select @sql2=@sql2+',sum(case object when '''+object+''' then 1 else 0 end) ['+object+']'
from a group by object
set @sql2='select ''count'' sort,case type when 0 then ''B'' when 2 then ''D'' when 3 then ''E'' when 4 then ''F'' when 5 then ''G'' end type'
+@sql2+' from a group by type'
exec(@sql1+' union all '+@sql2+' order by type,sort desc')
drop table a
#3
--测试数据
create table tb(id int,type int,object varchar(10),[money] int)
insert tb select 1,0,'AAA',100
union all select 2,2,'AAA',100
union all select 3,3,'BBB',100
union all select 4,4,'BBB',100
union all select 5,5,'CCC',100
union all select 6,3,'CCC',100
union all select 7,4,'DDD',100
union all select 8,0,'EEE',100
go
--统计处理
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then [money] else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)'+@s+'
from tb group by type')
go
--删除测试
drop table tb
/*--测试结果]
type AAA BBB CCC DDD EEE
---- ----------- ----------- ----------- ----------- -----------
B 100 0 0 0 100
D 100 0 0 0 0
E 0 100 100 0 0
F 0 100 0 100 0
G 0 0 100 0 0
--*/
create table tb(id int,type int,object varchar(10),[money] int)
insert tb select 1,0,'AAA',100
union all select 2,2,'AAA',100
union all select 3,3,'BBB',100
union all select 4,4,'BBB',100
union all select 5,5,'CCC',100
union all select 6,3,'CCC',100
union all select 7,4,'DDD',100
union all select 8,0,'EEE',100
go
--统计处理
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then [money] else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)'+@s+'
from tb group by type')
go
--删除测试
drop table tb
/*--测试结果]
type AAA BBB CCC DDD EEE
---- ----------- ----------- ----------- ----------- -----------
B 100 0 0 0 100
D 100 0 0 0 0
E 0 100 100 0 0
F 0 100 0 100 0
G 0 0 100 0 0
--*/
#4
select max(case when type=0 then 'A' when type=2 then 'B' when type=3 then 'C' end),sum(case when object='aaa' then money end),sum(case when object='aaa' then 1 else 0 end),
sum(case when object='bbb' then money end),sum(case when object='bbb' then 1 else 0 end),
sum(case when object='ccc' then money end),sum(case when object='ccc' then 1 else 0 end),
sum(case when object='ddd' then money end),sum(case when object='ddd' then 1 else 0 end) from A group by type
sum(case when object='bbb' then money end),sum(case when object='bbb' then 1 else 0 end),
sum(case when object='ccc' then money end),sum(case when object='ccc' then 1 else 0 end),
sum(case when object='ddd' then money end),sum(case when object='ddd' then 1 else 0 end) from A group by type
#5
具体要显示什么,自己改改就行了
#6
能不能同时显示 count() 和 sum()?
谢谢大家先!
谢谢大家先!
#7
这样的格式:
type AAA BBB CCC DDD
B sum(money) sum(money) sum(money) sum(money)
B count() count() count() count()
D sum(money) sum(money) sum(money) sum(money)
D count() count() count() count()E
F sum(money) sum(money) sum(money) sum(money)
F count() count() count() count()
G sum(money) sum(money) sum(money) sum(money)
G count() count() count() count()
type AAA BBB CCC DDD
B sum(money) sum(money) sum(money) sum(money)
B count() count() count() count()
D sum(money) sum(money) sum(money) sum(money)
D count() count() count() count()E
F sum(money) sum(money) sum(money) sum(money)
F count() count() count() count()
G sum(money) sum(money) sum(money) sum(money)
G count() count() count() count()
#8
2楼就是啊,运行一下就知道了
#9
--没看到count的要求,稍改一下就行了.
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then case when b.id=1 then [money] else 1 end else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)'+@s+'
from tb,(select id=1 union all select 2)b group by b.id,type')
go
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then case when b.id=1 then [money] else 1 end else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)'+@s+'
from tb,(select id=1 union all select 2)b group by b.id,type')
go
#10
--测试数据
create table tb(id int,type int,object varchar(10),[money] int)
insert tb select 1,0,'AAA',100
union all select 2,2,'AAA',100
union all select 3,3,'BBB',100
union all select 4,4,'BBB',100
union all select 5,5,'CCC',100
union all select 6,3,'CCC',100
union all select 7,4,'DDD',100
union all select 8,0,'EEE',100
go
--统计处理
declare @s varchar(8000)
set @s=''
select @s=@s+',['+rtrim(object)+']=sum(case object when '''
+rtrim(object)+''' then case when b.id=1 then [money] else 1 end else 0 end)'
from tb group by object
exec('
select type=substring(''BDEFG'',case type when 0 then 1 else type end,1)+b.name'+@s+'
from tb,(select id=1,name=''_sum'' union all select 2,''_count'')b group by type,b.id,b.name')
go
--删除测试
drop table tb
/*--测试结果]
type AAA BBB CCC DDD EEE
-------- ----------- ----------- ----------- ----------- -----------
B_sum 100 0 0 0 100
B_count 1 0 0 0 1
D_sum 100 0 0 0 0
D_count 1 0 0 0 0
E_sum 0 100 100 0 0
E_count 0 1 1 0 0
F_sum 0 100 0 100 0
F_count 0 1 0 1 0
G_sum 0 0 100 0 0
G_count 0 0 1 0 0
--*/
#11
问个巨菜的问题,,,单位器材器材资料是.dbf的,我在sql server2000里怎么导入这个这些表?望高手不吝解答,步骤给的清晰明了点,谢谢了
#12
收藏之
#13
怎么动态语句老是不懂啊,郁闷