表A (配件规格表)
规格编号 规格名称
001 30MM
002 40MM
表B (配件价格等级表)
价格编号 价格1 价格2
111 3 10
222 4 11
表C (配件订货表)
单号 规格 价格编号 价格1 价格2 数量
1 30MM 111 3 0 10
2 40MM 222 0 11 20
3 40MM 111 10 0 10 表D (商品价格表)
商品编号 商品规格 价格
0001 AAA 10
0002 BBB 20
表E (商品订货表)
单号 商品规格 数量
1 AAA 1
2 BBB 2
求一统计报表,格式如下:
(注:想根据表C (配件订货表)和表E (商品订货表)统计出各规格不同价格卖出的配件和商品的数量,
表头是根据规格表和价格表来动态生成的;表A(配件规格表)中的两条记录是不变的,两种规格对应表B
(配件价格等级表)的两种价格;也就是30MM对应价格1,40MM对应价格2)
30MM|3元 30MM|4元 40MM|10元 40MM|11元 商品|AAA 商品|BBB
10 0 10 20 1 2 (此行是数量)
30 0 100 220 10 40 (此行是金额)(以下是表结构创建代码,我试了很久但是一直没有成,请大家教教我,谢谢!)create table A(规格编号 char(10), 规格名称 varchar(20))
insert A select '001', '30MM'
union all select '002', '40MM'create table B(价格编号 char(10), 价格1 float,价格2 Float)
insert B select '111', '3','10'
union all select '222', '4','11'create table C(单号 char(10), 规格 varchar(20),价格编号 char(10),
价格1 float,价格2 Float,数量 int )
insert C select '1', '30MM','111','3','0','10'
union all select '2', '40MM','222','0','11','20'
union all select '3', '40MM','111','10','0','10'create table D(商品编号 char(10), 商品规格 varchar(20),价格 float )
insert D select '0001', 'AAA','10'
union all select '0002', 'BBB','20'create table E(单号 char(10), 商品规格 varchar(20),数量 int )
insert E select '1', 'AAA','1'
union all select '2', 'BBB','2'
规格编号 规格名称
001 30MM
002 40MM
表B (配件价格等级表)
价格编号 价格1 价格2
111 3 10
222 4 11
表C (配件订货表)
单号 规格 价格编号 价格1 价格2 数量
1 30MM 111 3 0 10
2 40MM 222 0 11 20
3 40MM 111 10 0 10 表D (商品价格表)
商品编号 商品规格 价格
0001 AAA 10
0002 BBB 20
表E (商品订货表)
单号 商品规格 数量
1 AAA 1
2 BBB 2
求一统计报表,格式如下:
(注:想根据表C (配件订货表)和表E (商品订货表)统计出各规格不同价格卖出的配件和商品的数量,
表头是根据规格表和价格表来动态生成的;表A(配件规格表)中的两条记录是不变的,两种规格对应表B
(配件价格等级表)的两种价格;也就是30MM对应价格1,40MM对应价格2)
30MM|3元 30MM|4元 40MM|10元 40MM|11元 商品|AAA 商品|BBB
10 0 10 20 1 2 (此行是数量)
30 0 100 220 10 40 (此行是金额)(以下是表结构创建代码,我试了很久但是一直没有成,请大家教教我,谢谢!)create table A(规格编号 char(10), 规格名称 varchar(20))
insert A select '001', '30MM'
union all select '002', '40MM'create table B(价格编号 char(10), 价格1 float,价格2 Float)
insert B select '111', '3','10'
union all select '222', '4','11'create table C(单号 char(10), 规格 varchar(20),价格编号 char(10),
价格1 float,价格2 Float,数量 int )
insert C select '1', '30MM','111','3','0','10'
union all select '2', '40MM','222','0','11','20'
union all select '3', '40MM','111','10','0','10'create table D(商品编号 char(10), 商品规格 varchar(20),价格 float )
insert D select '0001', 'AAA','10'
union all select '0002', 'BBB','20'create table E(单号 char(10), 商品规格 varchar(20),数量 int )
insert E select '1', 'AAA','1'
union all select '2', 'BBB','2'
这些是列转行后的字段名?
go
insert test values('张三','语文',80)
insert test values('张三','数学',86)
insert test values('张三','英语',75)
insert test values('李四','语文',78)
insert test values('李四','数学',85)
insert test values('李四','英语',78)想变成姓名 语文 数学 英语
张三 80 86 75
李四 78 85 78
declare @sql varchar(8000)
set @sql = 'select name'
select @sql = @sql + ',sum(case km when '''+km+''' then cj end) ['+km+']'
from (select distinct km from test) as a
select @sql = @sql+' from test group by name'
exec(@sql)drop table test