纵表格式如图所示: 查询sql语句如下: ),content)content,Date from SummerChina ' 变成横表如图所示: 纵表变横表sql语句如下: select Time, max(case Date when 'Morning' then content else null end) as Morning, max(case Date when 'Afternoon' then content else null end)as Afternoon ),content)…
竖表转横表 竖表结构: Name Course Grade 张三 语文 75 张三 数学 80 张三 英语 90 李四 语文 95 李四 数学 55 转换后横表结构: Name 语文 数学 英语 张三 75 80 90 李四 95 55 0 SQL语句: select Name, end) as 语文, end) as 数学, end) as 英语 from TableA group by Name 竖表转横表 横表转竖表 横表结构: ID Name Chinese Math English 1…
1.纵表转横表: 纵表结构:Table1 转换后的横表结构: Sql示例代码: select username, sum(case Course when '语文' then Grade else 0 end) as 语文, sum(case Course when '数学' then Grade else 0 end) as 数学, sum(case Course when '英语' then Grade else 0 end) as 英语 from Table1 group by usern…
1,纵表转横表 纵表结构 Table_A: 转换后的结构: 纵表转横表的SQL示例: SELECT Name , SUM(CASE WHEN Course = N'语文' THEN Grade ELSE 0 END) AS Chinese , SUM(CASE WHEN Course = N'数学' THEN Grade ELSE 0 END) AS Math…
1.数据准备 create table Vertical( Id int , ProjectName varchar(20), ProjectValue int ) insert into Vertical values (101,'旅游',100)insert into Vertical values (101,'牧业',101)insert into Vertical values (101,'工业',102)insert into Vertical values…
表名:sales SELECT NAME, sum( CASE MONTH WHEN '一月份' THEN money ELSE END ) AS '一月份', sum( CASE MONTH WHEN '二月份' THEN money ELSE END ) AS '二月份', sum( CASE MONTH WHEN '三月份' THEN money ELSE END ) AS '三月份' FROM sales GROUP BY NAME; 结果:…
现有数据如下: Sql: select a.MODELID, max( case a.PNAME when'计划开始' then a.PVALUE end) as RStart, max( case a.PNAME when '计划结束' then a.PVALUE end) as Rend from TB_CONSTRUCTION_PROPERTY as a group by a.MODELID 结果:…