竖表转横表 竖表结构: 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…
纵表格式如图所示: 查询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)…
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…
现有数据如下: 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 结果:…
) set @sql='select t3.BID,t5.UnitName,Sort,UnitTypeSort' select @sql=@sql+' , max(case t4.id when '''+ ID +''' then t3.Num else 0 end) ['+ Name +']' ') as a set @sql=@sql+' from DS_HZ_ResourceType t3 inner join Per_Dictionary t4 on t3.RID=t4.id left…