zoukankan      html  css  js  c++  java
  • (转)行变列SQL语句(MSSQL)

    SQL交叉表实例

    很简单的一个东西,见网上好多朋友问“怎么实现交叉表?”,以下是我写的一个例子,数据库基于SQL SERVER 2000。

    -- ======================================================
    --交叉表实例
    -- ======================================================

    建表:

    在查询分析器里运行:
    CREATE TABLE [Test] (
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [Source] [numeric](18, 0) NULL
    ) ON [PRIMARY]
    GO

    INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)

    Go

    交叉表语句的实现:
    --用于:交叉表的列数是确定的(用了这个很不错!)
    select name,sum(case subject when '数学' then source else 0 end) as '数学',
    sum(case subject when '英语' then source else 0 end) as '英语',
    sum(case subject when '语文' then source else 0 end) as '语文'
    from test
    group by name


    --用于:交叉表的列数是不确定的

    不确定列,字段2的值范围不固定。

    create table a
    (id VARchar(4),
    F2 VARCHAR(2),
    F3 INT)

    insert a(ID,F2,F3)
    select 'A001','01',12
    UNION ALL SELECT 'A001','02',24
    UNION ALL SELECT 'A002','01',10
    UNION ALL SELECT 'A002','03',5
    UNION ALL SELECT 'A003','02',6
    UNION ALL SELECT 'A004','04',13


    --SELECT * FROM A


    --不确定列数
    declare @sql varchar(8000)
    set @sql = 'select ID,'
    select @sql = @sql + 'sum(case F2 when ''' +F2+'''
    then F3 else '''' end) as '''+ '字段('+F2+')'','
    from (select distinct F2 from A) as B
    select @sql = left(@sql,len(@sql)-1) + ' from A group by ID'

    --PRINT @SQL
    exec(@sql)

    go
    ==============================================================================
    交叉表:
    CREATE TABLE Test(字段1 VARCHAR(10),字段2 VARCHAR(10),字段3 VARCHAR(10))
    INSERT Test SELECT 'a001', '01' , 12
    UNION ALL SELECT 'a001', '02' , 24
    UNION ALL SELECT 'a002', '01' , 10
    UNION ALL SELECT 'a002', '03' , 5
    UNION ALL SELECT 'a003', '02' , 6
    UNION ALL SELECT 'a004', '04' , 13


    SELECT * FROM Test

    SELECT 字段1,
    [字段(01)]=MAX(CASE WHEN 字段2 ='01' THEN 字段3 ELSE '' END),
    [字段(02)]=MAX(CASE WHEN 字段2 ='02' THEN 字段3 ELSE '' END),
    [字段(03)]=MAX(CASE WHEN 字段2 ='03' THEN 字段3 ELSE '' END),
    [字段(04)]=MAX(CASE WHEN 字段2 ='04' THEN 字段3 ELSE '' END)
    FROM Test
    GROUP BY 字段1
  • 相关阅读:
    Mysql 免密码登录,修改密码及忘记密码操作
    CentOS 6.9 升级MySQL 5.6.36到5.7.18
    【转载】详解布隆过滤器的原理、使用场景和注意事项
    AssemblyVersion、AssemblyFileVersion、AssemblyInformationalVersion 区别
    为什么要有财务自由【转】
    CacheManager.Core
    雪花算法,生成分布式唯一ID
    监控
    什么是并行、并发, 两者的区别是什么
    Emit用法
  • 原文地址:https://www.cnblogs.com/jackxia/p/821963.html
Copyright © 2011-2022 走看看