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

    示例:

    原本是同一个学生的2条记录,把它行转列后:

    select                    studentnid                           
                                 ,max(case StudentSourceTypeNid when'1' then SourcePrimaryKey else '0' end) vipSourcePrimaryKey,
                                 max(case StudentSourceTypeNid when'1' then SourceDate else null end) vipSourceDate, 
                                 max(case StudentSourceTypeNid when'2' then SourcePrimaryKey else '0' end) stmsSourcePrimaryKey,
                                 max(case StudentSourceTypeNid when'2' then SourceDate else null end) stmsSourceDate,
                                 max(case StudentSourceTypeNid when'3' then SourcePrimaryKey else '0' end) webSourcePrimaryKey,
                                 max(case StudentSourceTypeNid when'3' then SourceDate else null end) webSourceDate
                                from 
                                 (
                                 select student.*,
                                stuSource.SourcePrimaryKey,
                                stuSource.SourceDate,
                                stuSource.StudentSourceTypeNid
                                 from dbo.MEM_Student as student
                                inner join dbo.MEM_StudentSource as stuSource
                                on student.studentnid=stuSource.studentnid
                                 ) as tb
                                where studentnid=@studentnid
                                group by studentnid,studentname,sex

    成了这样的效果:

    以上就是代码了,我懂的。

  • 相关阅读:
    mysql分表技术(学习心得)
    Linux下搭建DNS服务器
    php简单单例模式
    JQuery的ajaxFileUpload图片上传初试
    Binary Tree Level Order Traversal II
    Remove Element
    Symmetric Tree
    Balanced Binary Tree
    Power of Two
    Merge Two Sorted Lists
  • 原文地址:https://www.cnblogs.com/pigddyou/p/2734091.html
Copyright © 2011-2022 走看看