zoukankan      html  css  js  c++  java
  • SQL2005行转列

    SQL SERVER 2005中新增加了两个关系运算符 PIVOT/ UNPIVOT,能够实现表中的列转换到行,以及行到列的转换工作。

    举例,还是先创建测试数据表

    CREATE TABLE sales.salesByMonth
    (
    year char(4),
    month char(3),
    amount 
    money,
    PRIMARY KEY (yearmonth)
    )

    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Jan'789.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Feb'389.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Mar'8867.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Apr'778.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','May'78.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Jun'9.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Jul'987.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Aug'866.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Sep'7787.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Oct'85576.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Nov'855.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2004','Dec'5878.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2005','Jan'7.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2005','Feb'6868.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2005','Mar'688.0000)
    INSERT INTO sales.salesByMonth (yearmonth, amount)
    VALUES('2005','Apr'9897.0000)

    我们想要得到类似这样的结果:
    Year  Jan            Feb             Mar       ..............
    ----- ---------- ----------- -----------
    2004 789.0000 389.0000     8867.0000 .............
    2005 7.0000     6868.0000   688.0000 ..............

    上一篇文章介绍的方法当然可以实现,但现在这里想要的列是固定的,不是动态的,就是12个月,所以也可以这样子来用:
    SELECT year,
    SUM(case when month = 'Jan' then amount else 0 endAS 'Jan',
    SUM(case when month = 'Feb' then amount else 0 endAS 'Feb',
    SUM(case when month = 'Mar' then amount else 0 endAS 'Mar',
    SUM(case when month = 'Apr' then amount else 0 endAS 'Apr',
    SUM(case when month = 'May' then amount else 0 endAS 'May',
    SUM(case when month = 'Jun' then amount else 0 endAS 'Jun',
    SUM(case when month = 'Jul' then amount else 0 endAS 'Jul',
    SUM(case when month = 'Aug' then amount else 0 endAS 'Aug',
    SUM(case when month = 'Sep' then amount else 0 endAS 'Sep',
    SUM(case when month = 'Oct' then amount else 0 endAS 'Oct',
    SUM(case when month = 'Nov' then amount else 0 endAS 'Nov',
    SUM(case when month = 'Dec' then amount else 0 endAS 'Dec'
    FROM sales.salesByMonth
    GROUP by year


    但这样事实上还是相当麻烦的,现在SQLSERVER2005中有更方便的实现方法。
    SELECT Year,[Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec]
    FROM (
      
    SELECT year, amount, month
      
    FROM sales.salesByMonth ) AS salesByMonth
    PIVOT ( 
    SUM(amount) FOR month IN
      (
    [Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
      ) 
    AS ourPivot
    ORDER BY Year

    就是这样,很简单的用法,效果是完全一样的。

    我们再尝试一下把year去掉:
    SELECT [Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec]
    FROM ( SELECT amount, month
    FROM sales.salesByMonth ) AS salesByMonth
    PIVOT ( 
    SUM(amount) FOR month IN
    (
    [Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
    AS ourPivot


    得到的结果是:
    Jan                 Feb                 Mar             ...
    ---------- ------------ -----------
    796.0000     7257.0000     9555.0000     ...

    同一个月份的数据累加到一起。


    再给个微软官方的例子:

  • 相关阅读:
    【转】 url中文乱码问题
    [转]Jquery 点击图片在弹出层显示大图
    JQuery获取和设置Select选项的常用方法总结
    springMVC框架下返回json格式的对象,list,map
    sqlserver数据库 表中字段值有空格,如何去除空格(例如char (5) 存入数据不足5位时sqlserver会自动补空格)
    jquery Jbox 插件实现弹出窗口在修改的数据之后,关闭弹出窗口刷新父页面的问题
    sqlserver 2008 r2 直接下载地址,可用迅雷下载
    web服务器与tomcat
    xml入门与解析
    jdbc框架-dbutils的简单使用
  • 原文地址:https://www.cnblogs.com/wenjl520/p/1619897.html
Copyright © 2011-2022 走看看