SQL行列转换

SQL Server:
参考:http://hi.baidu.com/qzrc/blog/item/952686184634e0b44aedbcea.html或http://www.cppblog.com/lilac/archive/2008/01/13/41069.html

--create table dbo.test_course(name varchar(20),subject varchar(20),score int);
insert into test_course values('L','java',81);
insert into test_course values('L','C',71);
insert into test_course values('M','java',66);
insert into test_course values('M','C',99);
insert into test_course values('N','java',33);
insert into test_course values('N','C',88);

--select * from test_course;
--drop table test_course;

select DISTINCT  name,
	(select score from test_course where subject='java' and name=t1.name) as java, 
	(select score from test_course where subject='C' and name=t1.name) as C
 from test_course t1

--行列转换
create table cj  --创建表cj
(
    ID       Int IDENTITY (1,1)     not null, --创建列ID,并且每次新增一条记录就会加1
    Name     Varchar(50),  
    Subject  Varchar(50),
    Result   Int, 
    primary key (ID)      --定义ID为表cj的主键     
);
--Truncate table cj
--Select * from cj
Insert into cj
Select '张三','语文',80 union all
Select '张三','数学',90 union all
Select '张三','物理',85 union all
Select '李四','语文',85 union all
Select '李四','物理',82 union all
Select '李四','英语',90 union all
Select '李四','政治',70 union all
Select '王五','英语',90
--行列转换
Declare @sql varchar(8000)
Set @sql = 'Select Name as 姓名'
Select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result else 0 end) ['+Subject+']'
from (select distinct Subject from cj) as cj  --把所有唯一的科目的名称都列举出来
Select @sql = @sql+' from cj group by name'
Exec (@sql)



create table dbo.test_sale(name varchar(20),subject varchar(20),sale_money int); 
insert into test_sale values('A','市场',1000);  
insert into test_sale values('A','配套',2000); 
insert into test_sale values('A','外贸',2000); 
insert into test_sale values('B','市场',500);  
insert into test_sale values('B','配套',1500); 
insert into test_sale values('B','外贸',1000); 
insert into test_sale values('C','市场',500);  
insert into test_sale values('C','配套',600); 
insert into test_sale values('C','外贸',1000); 


 select DISTINCT  name,  
     (select sum(sale_money) from test_sale where subject<>'外贸' and name=t1.name) as 国内销量,   
     (select sum(sale_money) from test_sale where subject='外贸' and name=t1.name) as 出口销量  
  from test_sale t1 
  

select name,
       sum(case when subject<>'外贸' then sale_money else 0 end) as 国内销量,
       sum(case when subject='外贸' then sale_money else 0 end) as 出口销量
from test_sale group by name

 drop table test_sale;  


参考http://topic.csdn.net/u/20090805/16/a0c3b692-4488-45ba-a919-14f4a812521b.html?seed=700138887&r=58862742#r_58862742
今日做销售数据统计时遇到一个棘手问题,请各位朋友不吝赐教
原数据格式如下:
产品    销量  销售类型
A      1000      市场
A      2000      配套
A      2000      外贸
B      500        市场
B      1500      配套
B      1000      外贸
C        500      市场
C        600      配套
C        1000      外贸
……    ……      ……

如何通过 sql 查询语句得到如下结果(sql 2000)

产品      国内销量  出口销量
A        3000      2000
B        2000      1000
C        1100        1000

你可能感兴趣的:(sql,C++,c,SQL Server,C#)