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

    初始化数据
    select * into #temp from 
    (
    select Name = 'aaa', Course = 'English', Score = 95 union all
    select Name = 'aaa', Course = 'Chinese', Score = 82 union all
    select Name = 'aaa', Course = 'History', Score = 77 union all
    select Name = 'bbb', Course = 'History', Score = 92 union all
    select Name = 'bbb', Course = 'English', Score = 66 union all
    select Name = 'bbb', Course = 'Chinese', Score = 100
    as tb




    方法一,使用聚合函数,SUM,MAX,MIN等都一样
    -- Line to Column

    select Name, 
        Max(case when Course = 'English' then Score endas 'English',
        Max(case when Course = 'Chinese' then Score endas 'Chinese',
        Max(case when Course = 'History' then Score endas 'History'
        from #temp
        group by Name



    方法二, 使用Pivot翻转
    -- Use Pivot

    select 
        *
    from
        #temp
    pivot
    (
        max(Score)
        for Course
        in ([English][Chinese][History])
    ) b

    Refer to 

  • 相关阅读:
    Vue 路由组件
    编写第一个JavaScript程序
    JavaScript 介绍
    JavaScript
    前台数据库
    cookie
    js date string parse
    判断时间大小 yyyy-MM-dd 格式
    正则表达式替换
    测试计时器
  • 原文地址:https://www.cnblogs.com/chyspace/p/2482934.html
Copyright © 2011-2022 走看看