资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

mysql横表怎么转纵表,表格怎么横向转纵向

SQL中的统计加横表转纵表

根本没涉及横纵。

创新互联建站主要从事做网站、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务张店,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

select a.*,b.军人人数,c.武警人数 from

(select 所在位置,count(*) as 警察人数 from 表 where 职务='警察' group by 位置) as a left jion

(select 所在位置,count(*) as 军人人数 from 表 where 职务='军人' group by 位置) as b

on a.所在位置=b.所在位置 left jion

(select 所在位置,count(*) as 武警人数 from 表 where 职务='武警' group by 位置) as c on a.所在位置=c.所在位置

mysql 纵表转横表,高手请支招

你这个需要后台处理一下才可以,直接转换比较麻烦,效率也不高。因为你需要计算出每天的开始时间和结束时间insert into newtable select employeeID,days,MAX(cardTime) as endtime,Min(cardTime) as starttime from

table group by employeeID,days; 大概就这意思,自己在调试调试吧

如何将横表转换成纵表

将要改的行"复制",点击鼠标右键,"选择性粘贴",在对话框中选择最下面的"转置",就可以实现你想要的结果

横纵表的相互转换!

--行列互转

/******************************************************************************************************************************************************

以学生成绩为例子,比较形象易懂

整理人:中国风(Roy)

日期:2008.06.06

******************************************************************************************************************************************************/

--1、行互列

-- -- (Roy)生成测试数据

if not object_id('Class') is null

drop table Class

Go

Create table Class([student] nvarchar(2),[subject] nvarchar(2),[grade] int)

Insert Class

select N'张三',N'语文',78 union all

select N'张三',N'数学',87 union all

select N'张三',N'英语',82 union all

select N'张三',N'物理',90 union all

select N'李四',N'语文',65 union all

select N'李四',N'数学',77 union all

select N'李四',N'英语',65 union all

select N'李四',N'物理',85

Go

--2000方法:

动态:

declare @s nvarchar(4000)

set @s=''

Select @s=@s+','+quotename([subject])+'=max(case when [subject]='+quotename([subject],'''')+' then [grade] else 0 end)'

from Class group by[subject]

exec('select [student]'+@s+' from Class group by [student]')

生成静态:

select

[student],

[数学]=max(case when [subject]='数学' then [grade] else 0 end),

[物理]=max(case when [subject]='物理' then [grade] else 0 end),

[英语]=max(case when [subject]='英语' then [grade] else 0 end),

[语文]=max(case when [subject]='语文' then [grade] else 0 end)

from

Class

group by [student]

GO

动态:

declare @s nvarchar(4000)

Select @s=isnull(@s+',','')+quotename([subject]) from Class group by[subject]

exec('select * from Class pivot (max([grade]) for [subject] in('+@s+'))b')

生成静态:

select *

from

Class

pivot

(max([grade]) for [subject] in([数学],[物理],[英语],[语文]))b

生成格式:

/*

student 数学 物理 英语 语文

------- ----------- ----------- ----------- -----------

李四 77 85 65 65

张三 87 90 82 78

(2 行受影响)

*/

------------------------------------------------------------------------------------------

go

--加上总成绩(学科平均分)

--2000方法:

动态:

declare @s nvarchar(4000)

set @s=''

Select @s=@s+','+quotename([subject])+'=max(case when [subject]='+quotename([subject],'''')+' then [grade] else 0 end)'

from Class group by[subject]

exec('select [student]'+@s+',[总成绩]=sum([grade]) from Class group by [student]')--加多一列(学科平均分用avg([grade]))

生成动态:

select

[student],

[数学]=max(case when [subject]='数学' then [grade] else 0 end),

[物理]=max(case when [subject]='物理' then [grade] else 0 end),

[英语]=max(case when [subject]='英语' then [grade] else 0 end),

[语文]=max(case when [subject]='语文' then [grade] else 0 end),

[总成绩]=sum([grade]) --加多一列(学科平均分用avg([grade]))

from

Class

group by [student]

go

--2005方法:

动态:

declare @s nvarchar(4000)

Select @s=isnull(@s+',','')+quotename([subject]) from Class group by[subject] --isnull(@s+',','') 去掉字符串@s中第一个逗号

exec('select [student],'+@s+',[总成绩] from (select *,[总成绩]=sum([grade])over(partition by [student]) from Class) a

pivot (max([grade]) for [subject] in('+@s+'))b ')

生成静态:

select

[student],[数学],[物理],[英语],[语文],[总成绩]

from

(select *,[总成绩]=sum([grade])over(partition by [student]) from Class) a --平均分时用avg([grade])

pivot

(max([grade]) for [subject] in([数学],[物理],[英语],[语文]))b

生成格式:

/*

student 数学 物理 英语 语文 总成绩

------- ----------- ----------- ----------- ----------- -----------

李四 77 85 65 65 292

张三 87 90 82 78 337

(2 行受影响)

*/

go

--2、列转行

-- -- (Roy)生成测试数据

if not object_id('Class') is null

drop table Class

Go

Create table Class([student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)

Insert Class

select N'李四',77,85,65,65 union all

select N'张三',87,90,82,78

Go

--2000:

动态:

declare @s nvarchar(4000)

select @s=isnull(@s+' union all ','')+'select [student],[subject]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all

+',[grade]='+quotename(Name)+' from Class'

from syscolumns where ID=object_id('Class') and Name not in('student')--排除不转换的列

order by Colid

exec('select * from ('+@s+')t order by [student],[subject]')--增加一个排序

生成静态:

select *

from (select [student],[subject]='数学',[grade]=[数学] from Class union all

select [student],[subject]='物理',[grade]=[物理] from Class union all

select [student],[subject]='英语',[grade]=[英语] from Class union all

select [student],[subject]='语文',[grade]=[语文] from Class)t

order by [student],[subject]

go

--2005:

动态:

declare @s nvarchar(4000)

select @s=isnull(@s+',','')+quotename(Name)

from syscolumns where ID=object_id('Class') and Name not in('student')

order by Colid

exec('select student,[subject],[grade] from Class unpivot ([grade] for [subject] in('+@s+'))b')

go

select

student,[subject],[grade]

from

Class

unpivot

([grade] for [subject] in([数学],[物理],[英语],[语文]))b

生成格式:

/*

student subject grade

------- ------- -----------

李四 数学 77

李四 物理 85

李四 英语 65

李四 语文 65

张三 数学 87

张三 物理 90

张三 英语 82

张三 语文 78

(8 行受影响)

*/

横表转纵表

select sum((case when name='序号' then type end)) 序号,

sum((case when name='截止日期' then type end)) 截止日期,

sum((case when name='品种' then type end)) 品种,

sum((case when name='种植进度' then type end)) 种植进度,

sum((case when name='当期' then type end)) 当期,

sum((case when name='5年平均' then type end)) 5年平均

from 表 gup by 1;

mysql纵表转横表

分两次进行不同的查询就可以实现了

先查出组员信息,比如组员数量、文章总数、评论总数等

再查出组长信息

两个查询一合并就可以了

SQL角本如下:

select a.用户名 组长, b.组员数量, b.文章总数, b.评论总数

from (select 编号, 用户名 from table_name where 角色 = '组长') a, -- 找组长信息

(select 组长, 

count(1) 组员数量,

sum(文章) 文章总数,

sum(评论) 评论总数

from table_name

where 组长 is not null

group by 组长) b   -- 找组员信息

where a.编号 = b.组长


网站名称:mysql横表怎么转纵表,表格怎么横向转纵向
文章转载:http://cdkjz.cn/article/dsehdgd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220