我做了一个实验,这个pivot就是实现行转列的,就可以,你看看:
create table table1(客户id varchar(20),日期 varchar(10),利润 numeric(10,2))insert into table1
select '广东','2007-3-1',10 union all
select '广东','2007-3-2',18 union all
select '湖北','2007-3-4',20 union all
select '湖南','2007-3-1',21 union all
select '湖南','2007-3-4',28
go
select [客户id],[2007-3-1],[2007-3-2],[2007-3-4]
from dbo.table1
PIVOT
(sum("利润")
for "日期"
in([2007-3-1],[2007-3-2],[2007-3-4])
) as A
/*
客户id 2007-3-1 2007-3-2 2007-3-4
广东 10.00 18.00 NULL
湖北 NULL NULL 20.00
湖南 21.00 NULL 28.00
*/
create table table1(客户id varchar(20),日期 varchar(10),利润 numeric(10,2))insert into table1
select '广东','2007-3-1',10 union all
select '广东','2007-3-2',18 union all
select '湖北','2007-3-4',20 union all
select '湖南','2007-3-1',21 union all
select '湖南','2007-3-4',28
go
select [客户id],[2007-3-1],[2007-3-2],[2007-3-4]
from dbo.table1
PIVOT
(sum("利润")
for "日期"
in([2007-3-1],[2007-3-2],[2007-3-4])
) as A
/*
客户id 2007-3-1 2007-3-2 2007-3-4
广东 10.00 18.00 NULL
湖北 NULL NULL 20.00
湖南 21.00 NULL 28.00
*/
(
客户id nchar(2),
订单id int,
"利润" float,
成本 float,
日期 date
)
insert into dbo.table1 values('湖南',2,1.2,1.1,'2007-03-01')
insert into dbo.table1 values('湖南',3,1.4,1.1,'2007-03-02')
insert into dbo.table1 values('湖南',4,1.7,1.6,'2007-03-02')
insert into dbo.table1 values('湖南',5,1.9,1.8,'2007-03-04')
insert into dbo.table1 values('湖北',6,10,9.9,'2007-03-02')
insert into dbo.table1 values('湖北',8,10.4,10.1,'2007-03-02')
insert into dbo.table1 values('湖北',9,10.7,10.6,'2007-03-02')
insert into dbo.table1 values('湖北',10,10.9,10.8,'2007-03-04')
insert into dbo.table1 values('广东',11,5,4.9,'2007-03-01')
insert into dbo.table1 values('广东',12,5.4,5.1,'2007-03-02')
insert into dbo.table1 values('广东',13,5.7,5.6,'2007-03-02')
insert into dbo.table1 values('广东',14,5.9,5.8,'2007-03-04')
insert into dbo.table1 values('广东',15,5.9,5.8,'2007-03-04')
insert into dbo.table1 values('湖南',2,1.2,1.1,'2007-03-01')
insert into dbo.table1 values('湖南',3,1.4,1.1,'2007-03-02')
insert into dbo.table1 values('湖南',4,1.7,1.6,'2007-03-02')
insert into dbo.table1 values('湖南',5,1.9,1.8,'2007-03-04')
insert into dbo.table1 values('湖北',6,10,9.9,'2007-03-02')
insert into dbo.table1 values('湖北',8,10.4,10.1,'2007-03-02')
insert into dbo.table1 values('湖北',9,10.7,10.6,'2007-03-02')
insert into dbo.table1 values('湖北',10,10.9,10.8,'2007-03-04')
insert into dbo.table1 values('广东',11,5,4.9,'2007-03-01')
insert into dbo.table1 values('广东',12,5.4,5.1,'2007-03-02')
insert into dbo.table1 values('广东',13,5.7,5.6,'2007-03-02')
insert into dbo.table1 values('广东',14,5.9,5.8,'2007-03-04')
insert into dbo.table1 values('广东',15,5.9,5.8,'2007-03-04')
得在外面,再套一层,才行,因为你还有其他的字段,导致了还是一行一行的数据create table dbo.table1
(
客户id nchar(2),
订单id int,
"利润" float,
成本 float,
日期 date
)insert into dbo.table1 values('湖南',1,1,0.9,'2007-03-01')
insert into dbo.table1 values('湖南',2,1.2,1.1,'2007-03-01')
insert into dbo.table1 values('湖南',3,1.4,1.1,'2007-03-02')
insert into dbo.table1 values('湖南',4,1.7,1.6,'2007-03-02')
insert into dbo.table1 values('湖南',5,1.9,1.8,'2007-03-04')
insert into dbo.table1 values('湖北',6,10,9.9,'2007-03-02')
insert into dbo.table1 values('湖北',8,10.4,10.1,'2007-03-02')
insert into dbo.table1 values('湖北',9,10.7,10.6,'2007-03-02')
insert into dbo.table1 values('湖北',10,10.9,10.8,'2007-03-04')
insert into dbo.table1 values('广东',11,5,4.9,'2007-03-01')
insert into dbo.table1 values('广东',12,5.4,5.1,'2007-03-02')
insert into dbo.table1 values('广东',13,5.7,5.6,'2007-03-02')
insert into dbo.table1 values('广东',14,5.9,5.8,'2007-03-04')
insert into dbo.table1 values('广东',15,5.9,5.8,'2007-03-04')
goselect 客户id,[2007-3-1],[2007-3-2],[2007-3-4]
from
(
select 客户id,"利润",日期 from dbo.table1
)t
PIVOT
(sum("利润")
for "日期"
in([2007-3-1],[2007-3-2],[2007-3-4])
) as A
/*
客户id 2007-3-1 2007-3-2 2007-3-4
广东 5 11.1 11.8
湖北 NULL 31.1 10.9
湖南 2.2 3.1 1.9
*/