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

    sqlserver的行转列 列转行问题

    行转列:
    1 使用Case when 方式

    CREATE TABLE [StudentScores]
    (
    [UserName] NVARCHAR(20), --学生姓名
    [Subject] NVARCHAR(30), --科目
    [Score] FLOAT, --成绩
    )

    INSERT INTO [StudentScores] SELECT 'Nick', '语文', 80

    INSERT INTO [StudentScores] SELECT 'Nick', '数学', 90

    INSERT INTO [StudentScores] SELECT 'Nick', '英语', 70

    INSERT INTO [StudentScores] SELECT 'Nick', '生物', 85

    INSERT INTO [StudentScores] SELECT 'Kent', '语文', 80

    INSERT INTO [StudentScores] SELECT 'Kent', '数学', 90

    INSERT INTO [StudentScores] SELECT 'Kent', '英语', 70

    INSERT INTO [StudentScores] SELECT 'Kent', '生物', 85


    SELECT * FROM [StudentScores]
    --如果我想知道每位学生的每科成绩,而且每个学生的全部成绩排成一行,这样方便
    我查看、统计,导出数据

    SELECT
    UserName,
    MAX(CASE Subject WHEN '语文' THEN Score ELSE 0 END) AS '语文',
    MAX(CASE Subject WHEN '数学' THEN Score ELSE 0 END) AS '数学',
    MAX(CASE Subject WHEN '英语' THEN Score ELSE 0 END) AS '英语',
    MAX(CASE Subject WHEN '生物' THEN Score ELSE 0 END) AS '生物'
    FROM dbo.[StudentScores]
    GROUP BY UserName
    2 使用PIVOT 、UNPIVOT运算符


    --方式一
    DECLARE @cmdText VARCHAR(8000);
    DECLARE @tmpSql VARCHAR(8000);

    SET @cmdText = 'SELECT CONVERT(VARCHAR(10), CreateTime, 120) AS
    CreateTime,' + CHAR(10);
    SELECT @cmdText = @cmdText + ' CASE PayType WHEN ''' + PayType + ''' THEN
    SUM(Money) ELSE 0 END AS ''' + PayType
    + ''',' + CHAR(10) FROM (SELECT DISTINCT PayType FROM
    Inpours ) T

    SET @cmdText = LEFT(@cmdText, LEN(@cmdText) -2) --注意这里,如果没有加CHAR
    (10) 则用LEFT(@cmdText, LEN(@cmdText) -1)

    SET @cmdText = @cmdText + ' FROM Inpours GROUP BY CreateTime, PayType
    ';

    SET @tmpSql ='SELECT CreateTime,' + CHAR(10);
    SELECT @tmpSql = @tmpSql + ' ISNULL(SUM(' + PayType + '), 0) AS ''' +
    PayType + ''',' + CHAR(10)
    FROM (SELECT DISTINCT PayType FROM Inpours ) T

    SET @tmpSql = LEFT(@tmpSql, LEN(@tmpSql) -2) + ' FROM (' + CHAR(10);

    SET @cmdText = @tmpSql + @cmdText + ') T GROUP BY CreateTime ';
    PRINT @cmdText
    EXECUTE (@cmdText);

    --方式二
    SELECT
    CreateTime,
    ISNULL(SUM([支付宝]) , 0) AS [支付宝] ,
    ISNULL(SUM([手机短信]) , 0) AS [手机短信] ,
    ISNULL(SUM([工商银行卡]), 0) AS [工商银行卡] ,
    ISNULL(SUM([建设银行卡]), 0) AS [建设银行卡]
    FROM
    (
    SELECT CONVERT(VARCHAR(10), CreateTime, 120) AS CreateTime,
    CASE PayType WHEN '支付宝' THEN SUM(Money) ELSE 0 END AS '支
    付宝' ,
    CASE PayType WHEN '手机短信' THEN SUM(Money) ELSE 0 END AS '手
    机短信',
    CASE PayType WHEN '工商银行卡' THEN SUM(Money) ELSE 0 END AS '工
    商银行卡',
    CASE PayType WHEN '建设银行卡' THEN SUM(Money) ELSE 0 END AS '建
    设银行卡'
    FROM Inpours
    GROUP BY CreateTime, PayType
    ) T
    GROUP BY CreateTime


    --方式三
    SELECT
    CreateTime, [支付宝] , [手机短信],
    [工商银行卡] , [建设银行卡]
    FROM
    (
    SELECT CONVERT(VARCHAR(10), CreateTime, 120) AS CreateTime,PayType,
    Money
    FROM Inpours
    ) P
    PIVOT (
    SUM(Money)
    FOR PayType IN
    ([支付宝], [手机短信], [工商银行卡], [建设银行卡])
    ) AS T
    ORDER BY CreateTime

    --列转行 主要通过 Union all ,max 来实现


    CREATE TABLE ProgrectDetail
    (
    ProgrectName NVARCHAR(20), --工程名称
    OverseaSupply INT, --海外供应商供给数量
    NativeSupply INT, --国内供应商供给数量
    SouthSupply INT, --南方供应商供给数量
    NorthSupply INT --北方供应商供给数量
    )

    INSERT INTO ProgrectDetail
    SELECT 'A', 100, 200, 50, 50
    UNION ALL
    SELECT 'B', 200, 300, 150, 150
    UNION ALL
    SELECT 'C', 159, 400, 20, 320
    UNION ALL
    SELECT 'D', 250, 30, 15, 15

    -- 使用 Union all 和max
    SELECT ProgrectName, 'OverseaSupply' AS Supplier,
    MAX(OverseaSupply) AS 'SupplyNum'
    FROM ProgrectDetail
    GROUP BY ProgrectName
    UNION ALL
    SELECT ProgrectName, 'NativeSupply' AS Supplier,
    MAX(NativeSupply) AS 'SupplyNum'
    FROM ProgrectDetail
    GROUP BY ProgrectName
    UNION ALL
    SELECT ProgrectName, 'SouthSupply' AS Supplier,
    MAX(SouthSupply) AS 'SupplyNum'
    FROM ProgrectDetail
    GROUP BY ProgrectName
    UNION ALL
    SELECT ProgrectName, 'NorthSupply' AS Supplier,
    MAX(NorthSupply) AS 'SupplyNum'
    FROM ProgrectDetail
    GROUP BY ProgrectName


    --用UNPIVOT方式
    SELECT ProgrectName,Supplier,SupplyNum
    FROM
    (
    SELECT ProgrectName, OverseaSupply, NativeSupply,
    SouthSupply, NorthSupply
    FROM ProgrectDetail
    )T
    UNPIVOT
    (
    SupplyNum FOR Supplier IN
    (OverseaSupply, NativeSupply, SouthSupply, NorthSupply )
    ) P

  • 相关阅读:
    牛儿
    Tarjan算法
    There is no resul…
    Struts2+JQuery+Json登陆实例
    struts2+jquery+easyui+datagrid+j…
    Spring:JdbcTemplate使用指南
    使用Spring的jdbcTemplate进一步简…
    JDBC连接MySQL数据库及示例
    PLSQL导入/导出数据方法
    PLSQ创建用户L
  • 原文地址:https://www.cnblogs.com/niuzaihenmang/p/5585026.html
Copyright © 2011-2022 走看看