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

     1 CREATE TABLE [dbo].[user](
     2     [id] [int] IDENTITY(1,1) NOT NULL,
     3     [name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL
     4 )
     5 
     6 
     7 CREATE TABLE [dbo].[score](
     8     [user_id] [int] NULL,
     9     [kemu] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    10     [fengshu] [float] NULL
    11 )
    12 
    13 
    14
    
    方法一:pivot
     SELECT name,[yuwen],[shuxue],isnull([yingyu],0) FROM dbo.score inner join dbo.[user] on id=user_id PIVOT (
    15     SUM(fengshu) FOR [kemu] IN ([yuwen],[shuxue],[yingyu])
    16 ) AS T 

    方法二:case when 

    select name,
           sum(case kemu when 'yuwen' then (fengshu) else 0 end ) as 'yuwen',
           sum(case kemu when 'shuxue' then (fengshu) else 0 end ) as 'shuxue',
           sum(case kemu when 'yingyu' then (fengshu) else 0 end ) as 'yingyu'
    from score inner join dbo.[user] on id=user_id group by name
  • 相关阅读:
    面向对象
    Vue + webpack 项目实践
    配置webstorm监听less时时转换
    delegate事件代理
    JS for 循环
    JS while 循环
    JS switch 分支语句
    JS if 判断
    JS 运算符
    JS typeof() parseInt() parseFloat()
  • 原文地址:https://www.cnblogs.com/szfhquan/p/2993714.html
Copyright © 2011-2022 走看看