数据库表
编号   交货时间       预计产值 实际产值
id     time           maxprice minprice 
1      2007-01-01     100000   80000
2      2007-01-03     200000   150000
3      2008-03-01     120000   100000
4      2008-04-3      150000   120000
5      2008-04-4      170000   150000

有如上一些数据,要求查询出如下格式的数据,假如查询条件是2008年的数据
月份         1      2       3      4      5      6      7     8       9     10      11      12
预计产值    。。   。。   120000 320000  。。   。。   。。   。。   。。   。。    。。    。。
实际产值    。。   。。   100000 270000  。。   。。   。。   。。   。。   。。    。。    。。
按年统计出每个月的预计总产值和实际总产值,请问这样的sql要怎么写?

解决方案 »

  1.   

    交叉数据报表
    有时候需要旋转结果以便在水平方向显示列,而在垂直方向显示行。这就是所谓的创建 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该 SELECT 语句还处理其中每个季度占多行的表。GROUP BY 语句将 Pivot 中一年的所有行合并成一行输出。当执行分组操作时,SUM 聚合中的 CASE 函数的应用方式是这样的:将每季度的 Amount 值添加到结果集的适当列中,在其它季度的结果集列中添加 0。如果该 SELECT 语句的结果用作电子表格的输入,那么电子表格将很容易计算每年的合计。当从应用程序使用 SELECT 时,可能更易于增强 SELECT 语句来计算每年的合计。例如:SELECT P1.*, (P1.Q1 + P1.Q2 + P1.Q3 + P1.Q4) AS YearTotal
    FROM (SELECT Year,
                 SUM(CASE P.Quarter WHEN 1 THEN P.Amount ELSE 0 END) AS Q1,
                 SUM(CASE P.Quarter WHEN 2 THEN P.Amount ELSE 0 END) AS Q2,
                 SUM(CASE P.Quarter WHEN 3 THEN P.Amount ELSE 0 END) AS Q3,
                 SUM(CASE P.Quarter WHEN 4 THEN P.Amount ELSE 0 END) AS Q4
         FROM Pivot AS P
         GROUP BY P.Year) AS P1
    GO
      

  2.   

    select *
    from 
    (
    select datepart(mm,[time])as '月份',sum(maxprice) as '预计产量'
     from class group by datepart(mm,[time]))A
    pivot(max([预计产量])for [月份] in([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))B
    union all
    select *
    from 
    (
    select datepart(mm,[time])as '月份',sum(maxprice) as '实际产量'
     from class group by datepart(mm,[time]))A
    pivot(max([实际产量])for [月份] in([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))B
      

  3.   


    --我的笨方法declare @tb table(id int,time datetime,maxprice int ,minprice int)Insert @tb select
    1   ,   '2007-01-01'    ,100000,  80000   union all select
    2   ,   '2007-01-03'   , 200000 , 150000  union all select
    3   ,   '2008-03-01'  ,  120000  ,100000  union all select
    4   ,   '2008-04-3 ' ,    150000 , 120000 union all select
    5   ,   '2008-04-4 ',     170000 , 150000 select [year]=convert(varchar(4),time,112),[字段2]='预计产值',
           sum(case month(time) when 1 then maxprice else 0 end) as 月1,
           sum(case month(time) when 2 then maxprice else 0 end) as 月2,
           sum(case month(time) when 3 then maxprice else 0 end) as 月3,
           sum(case month(time) when 4 then maxprice else 0 end) as 月4,
           sum(case month(time) when 5 then maxprice else 0 end) as 月5,
           sum(case month(time) when 6 then maxprice else 0 end) as 月6,
           sum(case month(time) when 7 then maxprice else 0 end) as 月7,
           sum(case month(time) when 8 then maxprice else 0 end) as 月8,
           sum(case month(time) when 9 then maxprice else 0 end) as 月9,
           sum(case month(time) when 10 then maxprice else 0 end) as 月10,
           sum(case month(time) when 11 then maxprice else 0 end) as 月11,
           sum(case month(time) when 12 then maxprice else 0 end) as 月12
    from @tb
    where convert(varchar(4),time,112)='2008'
    group by convert(varchar(4),time,112)
    union all
    select [year]=convert(varchar(4),time,112),[字段2]='实际产值',
           sum(case month(time) when 1 then minprice else 0 end) as 月1,
           sum(case month(time) when 2 then minprice else 0 end) as 月2,
           sum(case month(time) when 3 then minprice else 0 end) as 月3,
           sum(case month(time) when 4 then minprice else 0 end) as 月4,
           sum(case month(time) when 5 then minprice else 0 end) as 月5,
           sum(case month(time) when 6 then minprice else 0 end) as 月6,
           sum(case month(time) when 7 then minprice else 0 end) as 月7,
           sum(case month(time) when 8 then minprice else 0 end) as 月8,
           sum(case month(time) when 9 then minprice else 0 end) as 月9,
           sum(case month(time) when 10 then minprice else 0 end) as 月10,
           sum(case month(time) when 11 then minprice else 0 end) as 月11,
           sum(case month(time) when 12 then minprice else 0 end) as 月12
    from @tb
    where convert(varchar(4),time,112)='2008'
    group by convert(varchar(4),time,112)/*
    year     字段2         月1      月2      月3         ...............-------------------------------------------------------------------------------------------------------------------------- 
    2008 预计产值  0 0 120000 320000 0 0 0 0 0 0 0 0
    2008 实际产值 0 0 100000 270000 0 0 0 0 0 0 0 0
    */
      

  4.   

    2008年:
    select *
    from 
    (
    select datepart(mm,[time])as '月份',sum(maxprice) as '预计产量'
     from class where datepart(yyyy,[time])='2008'
     group by datepart(mm,[time]))A
    pivot(max([预计产量])for [月份] in([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))B
    union all
    select *
    from 
    (
    select datepart(mm,[time])as '月份',sum(maxprice) as '实际产量'
     from class where datepart(yyyy,[time])='2008'
     group by datepart(mm,[time]))A
    pivot(max([实际产量])for [月份] in([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))B
      

  5.   

    修改一下select '预计产量',*
    from 
    (
    select datepart(mm,[time])as '月份',sum(maxprice) as '预计产量'
     from @tb where convert(varchar(4),time,112)='2008'  group by datepart(mm,[time]))A
    pivot(max([预计产量])for [月份] in([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))B
    union all
    select '实际产量', *
    from 
    (
    select datepart(mm,[time])as '月份',sum(minprice) as '实际产量'
     from @tb where convert(varchar(4),time,112)='2008' group by datepart(mm,[time]))A
    pivot(max([实际产量])for [月份] in([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))B