Welcome 微信登录

首页 / 数据库 / MySQL / SQL 内置函数(pivot) 纵转横

1.创建表:create table test(id int,name varchar(20),quarter int,profile int))
insert into test values(1,"a",1,1000);
insert into test values(1,"a",2,1000);
insert into test values(1,"a",3,1000);
insert into test values(1,"a",4,1000);
insert into test values(2,"a",1,1000);
insert into test values(2,"a",2,1000);
insert into test values(2,"a",3,1000);
insert into test values(2,"a",4,1000);
2. Pivot 用法select id,name,[1] as "1th Season",[2] as "2th Season",[3] as "3th Season",[4] as "4th Season"
from test
pivot

sum(profile)
for quarter in ([1],[2],[3],[4])
)as pvt

select * from test


3.Unpivotselect * from test2
select id,name,quarters,profiles
from test2
unpivot

profiles
for quarters in ([Q1],[Q2],[Q3],[Q4])

as unpvt
Oracle 10g数据库基础之基本查询语句-上SQLite3 模糊查询相关资讯      SQL语句 
  • 如何定位SQL语句在共享池里用到了  (03月17日)
  • Java 注解入门 自动生成SQL语句  (07/28/2015 16:08:34)
  • Oracle 通过sql profile为sql语句  (05/03/2015 19:43:07)
  • MySQL 存储过程动态执行sql语句  (10/13/2015 19:10:08)
  • 画图解释 SQL join 语句  (07/17/2015 15:16:27)
  • MySQL数据库sql语句调优  (03/21/2015 17:42:45)
本文评论 查看全部评论 (0)
表情: 姓名: 字数