码迷,mamicode.com
首页 > 其他好文 > 详细

行转列,列转行

时间:2017-03-13 11:44:03      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ima   ble   not   chinese   rom   苹果   aos   test   identity   

 

oracle:

 

----------------------------- 行转列  --------------------------------
select * from demo
create table demo(id int,name varchar(20),nums int);  ---- 创建表
insert into demo values(1, ‘苹果‘, 1000);
insert into demo values(2, ‘苹果‘, 2000);
insert into demo values(3, ‘苹果‘, 4000);
insert into demo values(4, ‘橘子‘, 5000);
insert into demo values(5, ‘橘子‘, 3000);
insert into demo values(6, ‘葡萄‘, 3500);
insert into demo values(7, ‘芒果‘, 4200);
insert into demo values(8, ‘芒果‘, 5500);

select * from (select name, nums from demo) pivot (sum(nums) for name in (‘苹果‘ 苹果, ‘橘子‘, ‘葡萄‘, ‘芒果‘));
 

----------------- 列转行 ----------------------
create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);
 
insert into Fruit values(1,‘苹果‘,1000,2000,3300,5000);
insert into Fruit values(2,‘橘子‘,3000,3000,3200,1500);
insert into Fruit values(3,‘香蕉‘,2500,3500,2200,2500);
insert into Fruit values(4,‘葡萄‘,1500,2500,1200,3500);
 
 
select * from Fruit
 

select id , name, jidu, xiaoshou from Fruit unpivot (xiaoshou for jidu in (q1, q2, q3, q4) )
 
 
 
 
sqlserver:
-----------------------行转列----------------------------------
CREATE TABLE [dbo].[tbl_Student](
[ID] [int] IDENTITY(1,1) NOT NULL,
[学生ID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[学生姓名] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[课程ID] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[课程名称] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[成绩] [int] NULL,
[教师ID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[教师姓名] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]


insert into tbl_Student values(
‘S3‘,‘王五‘,‘K4 ‘,‘政治‘,53,‘T4‘,‘赵老师‘);
insert into tbl_Student values(
‘S1‘,‘ 张三 ‘,‘K1 ‘,‘数学‘,61,‘T1‘,‘张老师‘);
insert into tbl_Student values(
‘S2‘,‘李四‘,‘K3 ‘,‘英语‘,88,‘T3‘,‘李老师‘);
insert into tbl_Student values(
‘S1‘,‘ 张三 ‘,‘K4 ‘,‘政治‘,77,‘T4‘,‘赵老师‘);
insert into tbl_Student values(
‘S2‘,‘李四‘,‘K4 ‘,‘政治‘,67,‘T5‘,‘周老师‘);
insert into tbl_Student values(
‘S3‘,‘王五‘,‘K2 ‘,‘语文‘,90,‘T2‘,‘王老师‘);
insert into tbl_Student values(
‘S3‘,‘王五‘,‘K1 ‘,‘数学‘,55,‘T1‘,‘张老师‘);
insert into tbl_Student values(
‘S1‘,‘ 张三 ‘,‘K2 ‘,‘语文‘,81,‘T2‘,‘王老师‘);
insert into tbl_Student values(
‘S4‘,‘赵六‘,‘K2 ‘,‘语文‘,59,‘T1‘,‘王老师‘);
insert into tbl_Student values(
‘S1‘,‘ 张三 ‘,‘K3 ‘,‘英语‘,37,‘T3‘,‘李老师‘);


select 学生姓名,课程ID,成绩,教师ID,教师姓名,
max(case 课程名称 when ‘政治‘ then 成绩 else 0 end) 政治,
max(case 课程名称 when ‘语文‘ then 成绩 else 0 end) 语文,
max(case 课程名称 when ‘数学‘ then 成绩 else 0 end) 数学,
max(case 课程名称 when ‘英语‘ then 成绩 else 0 end) 英语
from tbl_Student group by 学生姓名,课程ID,成绩,教师ID,教师姓名


select *
from (select * from tbl_Student)
a pivot (max(成绩) for 课程名称 in (语文,数学,英语,政治)) b




--------------------------列转行 -----------------------------------

CREATE TABLE [dbo].[tbl_test2](
    [UserID] [int] NULL,
[UserNo] [int] NULL,
[A] [int] NULL,
[B] [int] NULL,
[C] [int] NULL
) ON [PRIMARY]

insert into tbl_test2 values(1,1,11,22,33);

SELECT  USERID,USERNO,tType=attribute
FROM (select * from tbl_列转行测试)a
UNPIVOT
(
value FOR attribute IN(A, B,C)
) AS UPV




行转列,列转行

标签:ima   ble   not   chinese   rom   苹果   aos   test   identity   

原文地址:http://www.cnblogs.com/8899man/p/6541609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!