zoukankan      html  css  js  c++  java
  • SQL 行转列 PIVOT 学习示例

    CREATE TABLE [StudentScores]
    (
    [UserName] NVARCHAR(20), --学生姓名
    [Subject] NVARCHAR(30), --科目
    [Score] FLOAT, --成绩
    )
    
    INSERT INTO [StudentScores] SELECT '张三', '语文', 80
    INSERT INTO [StudentScores] SELECT '张三', '数学', 90
    INSERT INTO [StudentScores] SELECT '张三', '英语', 70
    INSERT INTO [StudentScores] SELECT '张三', '生物', 85
    INSERT INTO [StudentScores] SELECT '李四', '语文', 80
    INSERT INTO [StudentScores] SELECT '李四', '数学', 92
    INSERT INTO [StudentScores] SELECT '李四', '英语', 76
    INSERT INTO [StudentScores] SELECT '李四', '生物', 88
    INSERT INTO [StudentScores] SELECT '码农', '语文', 60
    INSERT INTO [StudentScores] SELECT '码农', '数学', 82
    INSERT INTO [StudentScores] SELECT '码农', '英语', 96
    INSERT INTO [StudentScores] SELECT '码农', '生物', 78
    
    
    select * from [StudentScores]
    
    SELECT * FROM [StudentScores] /*数据源*/
    AS P 
    PIVOT 
    (
    SUM(Score/*行转列后 列的值*/) FOR 
    p.Subject/*需要行转列的列*/ IN ([语文],[数学],[英语],[生物]/*列的值*/)
    ) AS T where username<>'李四'
  • 相关阅读:
    shell脚本进阶
    sort与uniq命令
    sed命令
    DNS与CDN
    nginx
    Docker Private Registry
    docker存储卷
    docker容器网络配置
    docker容器网络
    docker容器虚拟化
  • 原文地址:https://www.cnblogs.com/wang0020/p/10190192.html
Copyright © 2011-2022 走看看