select 姓名 ,
sum(case  操作类型  when 类型1 then 1 esel 0 end) as 类型1,
sum(case  操作类型  when 类型1 then  金额 esel 0 end) as 类型1金额,
......,
日期from table
group by 姓名 ,日期
有时候需要旋转结果以便在水平方向显示列,而在垂直方向显示行。这就是所谓的创建 PivotTable®、创建交叉数据报表或旋转数据。假定有一个表 Pivot,其中每季度占一行。对 Pivot 的 SELECT 操作在垂直方向上列出这些季度:Year      Quarter      Amount
----      -------      ------
1990      1           1.1
1990      2           1.2
1990      3           1.3
1990      4           1.4
1991      1           2.1
1991      2           2.2
1991      3           2.3
1991      4           2.4生成报表的表必须是这样的,其中每年占一行,每个季度的数值显示在一个单独的列中,如:Year
 Q1
 Q2
 Q3
 Q4
 
1990
 1.1
 1.2
 1.3
 1.4
 
1991
 2.1
 2.2
 2.3
 2.4
 
下面的语句用于创建 Pivot 表并在其中填入第一个表中的数据:USE Northwind
GOCREATE TABLE Pivot
( Year      SMALLINT,
  Quarter   TINYINT, 
  Amount      DECIMAL(2,1) )
GO
INSERT INTO Pivot VALUES (1990, 1, 1.1)
INSERT INTO Pivot VALUES (1990, 2, 1.2)
INSERT INTO Pivot VALUES (1990, 3, 1.3)
INSERT INTO Pivot VALUES (1990, 4, 1.4)
INSERT INTO Pivot VALUES (1991, 1, 2.1)
INSERT INTO Pivot VALUES (1991, 2, 2.2)
INSERT INTO Pivot VALUES (1991, 3, 2.3)
INSERT INTO Pivot VALUES (1991, 4, 2.4)
GO下面是用于创建旋转结果的 SELECT 语句:SELECT Year, 
    SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1,
    SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2,
    SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3,
    SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4
FROM Northwind.dbo.Pivot
GROUP BY Year
GO

解决方案 »

  1.   

    create table tt3(姓名 varchar(10),  操作类型  varchar(10) ,  金额  int,    日期 datetime)insert tt3 select 
    '张三',   '类型1',       10     ,' 2004-10-12' union all select
    '张三' ,  '类型1 ',      10      ,'2004-10-12' union all select
    '张三'  , '类型2  ',     20      ,'2004-10-12' union all select
    '李四'   ,'类型1   ',    10    ,'  2004-10-12' union all select
    '李四'   ,'类型3    ',   30  ,'    2004-10-12' union all  select
    '李四'   ,'类型3     ',  30,'      2004-10-12'select 姓名,操作类型 ,count(1) as count ,sum(金额) as sum,日期 into #table from tt3
    group by 姓名,操作类型 ,日期
    order by 1,2,3select 姓名,max(case 操作类型 when '类型1' then count else 0  end) as 类型1数量,
    max(case 操作类型 when '类型1' then sum else 0  end) as 类型1金额,
    max(case 操作类型 when '类型2' then count else 0  end) as 类型2数量,
    max(case 操作类型 when '类型2' then sum else 0  end) as 类型2金额,
    max(case 操作类型 when '类型2' then count else 0  end) as 类型3数量,
    max(case 操作类型 when '类型2' then sum else 0  end) as 类型3金额,
    sum(count)as 总数量,sum(sum) as  总金额,日期 from #table
    group by 姓名,日期drop table #tabledrop table tt3
      

  2.   

    create table table10(姓名 char(8),操作类型 char(8),金额 int,日期 datetime)
    insert table10 values('张三','类型1',10,'2004-10-12')
    insert table10 values('张三','类型1',10,'2004-10-12')
    insert table10 values('张三','类型2',20,'2004-10-12')
    insert table10 values('李四','类型1',10,'2004-10-12')
    insert table10 values('李四','类型3',30,'2004-10-12')
    insert table10 values('李四','类型3',30,'2004-10-12')
    select 姓名 ,
    sum(case  操作类型  when '类型1' then 1 else 0 end) as 类型1,
    sum(case  操作类型  when '类型1' then  金额 else 0 end) as 类型1金额,sum(case  操作类型  when '类型2' then 1 else 0 end) as 类型2,
    sum(case  操作类型  when '类型2' then  金额 else 0 end) as 类型2金额,sum(case  操作类型  when '类型3' then 1 else 0 end) as 类型3,
    sum(case  操作类型  when '类型3' then  金额 else 0 end) as 类型3金额,sum(case 金额 when 金额 then 金额 else 0 end) as 金额,日期from table10
    group by 姓名 ,日期