zoukankan      html  css  js  c++  java
  • Sql Server 2005 行转列的实现(横排)

    SQL SERVER 2005中新增加了两个关系运算符 PIVOT/ UNPIVOT,能够实现表中的列转换到行,以及行到列的转换工作。
    举例,还是先创建测试数据表

    Create TABLE sales.salesByMonth 

    1. year char(4), 
    2. month char(3), 
    3. amount money, 
    4. PRIMARY KEY (year, month) 
    5. Insert INTO sales.salesByMonth (year, month, amount) 
    6. VALUES('2004','Jan', 789.0000) 
    7. Insert INTO sales.salesByMonth (year, month, amount) 
    8. VALUES('2004','Feb', 389.0000) 
    9. Insert INTO sales.salesByMonth (year, month, amount) 
    10. VALUES('2004','Mar', 8867.0000) 
    11. Insert INTO sales.salesByMonth (year, month, amount) 
    12. VALUES('2004','Apr', 778.0000) 
    13. Insert INTO sales.salesByMonth (year, month, amount) 
    14. VALUES('2004','May', 78.0000) 
    15. Insert INTO sales.salesByMonth (year, month, amount) 
    16. VALUES('2004','Jun', 9.0000) 
    17. Insert INTO sales.salesByMonth (year, month, amount) 
    18. VALUES('2004','Jul', 987.0000) 
    19. Insert INTO sales.salesByMonth (year, month, amount) 
    20. VALUES('2004','Aug', 866.0000) 
    21. Insert INTO sales.salesByMonth (year, month, amount) 
    22. VALUES('2004','Sep', 7787.0000) 
    23. Insert INTO sales.salesByMonth (year, month, amount) 
    24. VALUES('2004','Oct', 85576.0000) 
    25. Insert INTO sales.salesByMonth (year, month, amount) 
    26. VALUES('2004','Nov', 855.0000) 
    27. Insert INTO sales.salesByMonth (year, month, amount) 
    28. VALUES('2004','Dec', 5878.0000) 
    29. Insert INTO sales.salesByMonth (year, month, amount) 
    30. VALUES('2005','Jan', 7.0000) 
    31. Insert INTO sales.salesByMonth (year, month, amount) 
    32. VALUES('2005','Feb', 6868.0000) 
    33. Insert INTO sales.salesByMonth (year, month, amount) 
    34. VALUES('2005','Mar', 688.0000) 
    35. Insert INTO sales.salesByMonth (year, month, amount) 
    36. VALUES('2005','Apr', 9897.0000) 

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

    1. Select year, 
    2. SUM(case when month = 'Jan' then amount else 0 end) AS 'Jan', 
    3. SUM(case when month = 'Feb' then amount else 0 end) AS 'Feb', 
    4. SUM(case when month = 'Mar' then amount else 0 end) AS 'Mar', 
    5. SUM(case when month = 'Apr' then amount else 0 end) AS 'Apr', 
    6. SUM(case when month = 'May' then amount else 0 end) AS 'May', 
    7. SUM(case when month = 'Jun' then amount else 0 end) AS 'Jun', 
    8. SUM(case when month = 'Jul' then amount else 0 end) AS 'Jul', 
    9. SUM(case when month = 'Aug' then amount else 0 end) AS 'Aug', 
    10. SUM(case when month = 'Sep' then amount else 0 end) AS 'Sep', 
    11. SUM(case when month = 'Oct' then amount else 0 end) AS 'Oct', 
    12. SUM(case when month = 'Nov' then amount else 0 end) AS 'Nov', 
    13. SUM(case when month = 'Dec' then amount else 0 end) AS 'Dec'
    14. FROM sales.salesByMonth 
    15. GROUP by year


    但这样事实上还是相当麻烦的,现在SQLSERVER2005中有更方便的实现方法。

    Select Year,[Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec] 

    1. FROM ( 
    2. Select year, amount, month
    3. FROM sales.salesByMonth ) AS salesByMonth 
    4. PIVOT ( SUM(amount) FOR month IN
    5.   ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec]) 
    6.   ) AS ourPivot 
    7. orDER BY Year

    就是这样,很简单的用法,效果是完全一样的。
    我们再尝试一下把year去掉:

    Select [Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec] 

    1. FROM ( Select amount, month
    2. FROM sales.salesByMonth ) AS salesByMonth 
    3. PIVOT ( SUM(amount) FOR month IN
    4. ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec]) 
    5. ) AS ourPivot 

    得到的结果是:
    Jan                 Feb                 Mar             …
    ———- ———— ———–
    796.0000     7257.0000     9555.0000     …
    同一个月份的数据累加到一起。
    再给个微软官方的例子:

    image

  • 相关阅读:
    缓存概述
    进程Process
    MVC系统过滤器、自定义过滤器
    暂无,进程那篇深度不够
    SeasLog 与 monolog 日志系统的区别,SeasLog安装步骤
    阿里面试官:说一下从url输入到返回请求的过程,问的难度就是不一样!
    [技术分享]OSI七层模型详解
    Mysql引擎介绍及InnoDB逻辑存储结构
    Paypal 实现自动订阅
    PayPal 支付Checkout 收银台和 Subscription 订阅计划全过程分享
  • 原文地址:https://www.cnblogs.com/wuhen/p/1615187.html
Copyright © 2011-2022 走看看