当前位置:首页 » 编程语言 » sql列转行要求字段长度一样
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql列转行要求字段长度一样

发布时间: 2022-08-15 17:40:20

‘壹’ sql 列转行

你在SQL中写只能用存储过程,而且你的行和列数必须相等,或者可以在程序中控制写,我觉得在程序中比较好写一点,把所有数据查出来,在写个算法,把数据update 或者重新insert

‘贰’ sql行转列,列转行有哪个大佬会,教教我,并带有解释,谢谢

没有直接转换的,毕竟字段格式不一样。
但是有些办法,我举个栗子,你感受下。
Table A 有列
term,department ,empnum
2015 , 信息部 , 86
2015,人力部 , 20
2016,信息部 , 101
2016,销售部 , 50

我转换下,行是部门,列是年份,我要显示,每个部门多少人。
select k.depname , (select empnum from A where department = k.depname and term=2015) as 2015, (select empnum from A where department = k.depname and term=2016) as 2016 from
(select distinct(department) as depname from A ) k

结果就显示为
depname 2015 2016
信息部 86 101
人力部 20
销售部 50

‘叁’ 请教sql 列转行问题

select cOrder,cCode,

cl1=isnull((select sum(cl) from tb a where day(cdate)=1),0) and tb.corder=a.corder and tb.ccode=a.ccode),0),

cl2=isnull((select sum(cl) from tb a where day(cdate)=2),0) and tb.corder=a.corder and tb.ccode=a.ccode),0),

cl3=isnull((select sum(cl) from tb a where day(cdate)=3),0) and tb.corder=a.corder and tb.ccode=a.ccode),0),

cl4=isnull((select sum(cl) from tb a where day(cdate)=4),0) and tb.corder=a.corder and tb.ccode=a.ccode),0),

cl5=isnull((select sum(cl) from tb a where day(cdate)=5),0) and tb.corder=a.corder and tb.ccode=a.ccode),0),

cl6=isnull((select sum(cl) from tb a where day(cdate)=6),0) and tb.corder=a.corder and tb.ccode=a.ccode),0),

/*复制30行或31行,然后更改“日”Cxx和day(cdate)=x两个位置

.

.

.

*/

cl30=isnull((select sum(cl) from tb a where day(cdate)=30),0) and tb.corder=a.corder and tb.ccode=a.ccode),0),

合计=isnull((select sum(cl) from tb a where month(cdate)=6 and year(cdate)=2013 and tb.corder=a.corder and tb.ccode=a.ccode),0)

from tb where month(cdate)=6 and year(cdate)=2013


‘肆’ SQL2000行转列于列转行问题,急~~~ (部门是不确定几个的)

静态脚本
select '当月入职人数' as 项目
, case when 部门='行政部' then 当月入职人数 else null end as 行政部
, case when 部门='技术部' then 当月入职人数 else null end as 技术部
from 表名
union all
select '当月离职人数' as 项目
, case when 部门='行政部' then 当月离职人数 else null end as 行政部
, case when 部门='技术部' then 当月离职人数 else null end as 技术部
from 表名
union all
select '当月在职人数' as 项目
, case when 部门='行政部' then 当月在职人数 else null end as 行政部
, case when 部门='技术部' then 当月在职人数 else null end as 技术部
from 表名

改动态脚本(只改部门,即改原表行不定,列数目固定):
declare @sql nvarchar(4000)
set @sql=''
set @sql=@sql+'
select ''当月入职人数'' as 项目
'
select @sql=@sql+', case when 部门='''+部门+''' then 当月入职人数 else null end as '+部门
from 表名
set @sql=@sql+'
from 表名
'
set @sql=@sql+'union all
select ''当月离职人数'' as 项目
'
select @sql=@sql+', case when 部门='''+部门+''' then 当月离职人数 else null end as '+部门
from 表名
set @sql=@sql+'
from 表名
'
set @sql=@sql+'union all
select ''当月在职人数'' as 项目
'
select @sql=@sql+', case when 部门='''+部门+''' then 当月在职人数 else null end as '+部门
from 表名
set @sql=@sql+'
from 表名
'
exec sp_executesql @sql

改动态脚本(改原表行和列数目不固定,两层动态脚本,能实现但基本难读):
declare @sql nvarchar(4000)
set @sql='declare @sql_in nvarchar(4000)
set @sql_in='' '' '
set @sql=@sql+'select @sql_in=@sql_in+''union all
select ''''''+name+'''''' as 项目
'
select @sql=@sql+', case when 部门='''''+部门+''''' then ''+name+'' else null end as '+部门
from 表名
set @sql=@sql+'
from 表名
''
from syscolumns where id=(select id from sysobjects where name=''表名'')
order by colorder
set @sql_in=stuff(@sql_in,1,10,'''')
exec sp_executesql @sql_in
'
exec sp_executesql @sql

由于是sql2000,nvarchar类型只能最多4000个字,如果你部门太多,动态脚本长度肯定是不行的

‘伍’ SQL语句 列转行

-- ========================= PIVOT 行列转置 ===========================
-- 1、【行列转置PIVOT】
declare @Score table(StuNo varchar(10), StuName varchar(50), CourseName varchar(50), Score int)
insert into @Score
select '1', 'Tom', 'Math', 80 union all
select '1', 'Tom', 'English', 82 union all
select '1', 'Tom', 'Geography', 84 union all
select '2', 'Jone', 'Math', 79 union all
select '2', 'Jone', 'English', 88 union all
select '2', 'Jone', 'Geography',86
select * from @Score

SELECT StuNo, StuName, Math, English, [Geography]
FROM @Score PIVOT (MAX(Score) FOR CourseName in (Math, English, [Geography]) ) AS ScoreList
ORDER BY StuNo

-- 2、【列行转置UNPIVOT】
declare @ScoreList table(StuNo varchar(10), StuName varchar(50), Math int, English int, [Geography] int)
insert into @ScoreList
select '1', 'Tom', 80, 82, 84 union all
select '2', 'Jone', 79, 88, 86
select * from @ScoreList

SELECT StuNo, StuName, CourseName, Score
FROM @ScoreList UNPIVOT (Score FOR CourseName in (Math, English, [Geography]) ) AS ScorePvtTable
ORDER BY StuNo

‘陆’ sql 列转行

通用的方法用union,不同的数据库可能有对应的方法,比如sqlserver的unpovit


select'A'key,Avaluefromtabname
union
select'B'key,Bvaluefromtabname
union
select'C'key,Cvaluefromtabname
union
select'D'key,Dvaluefromtabname
union
select'E'key,Evaluefromtabname

‘柒’ sql语句列转行

我整理的行转列的问题:

--创建tb表
createtabletb(姓名varchar(10),课程varchar(10),分数int)
insertintotbvalues('张三','语文',74)
insertintotbvalues('张三','数学',83)
insertintotbvalues('张三','物理',93)
insertintotbvalues('李四','语文',74)
insertintotbvalues('李四','数学',84)
insertintotbvalues('李四','物理',94)
go

select*Fromtb

--SQLSERVER2000静态行转列
select姓名as姓名,
max(case课程when'语文'then分数elsenullend)语文,
max(case课程when'数学'then分数elsenullend)数学,
max(case课程when'物理'then分数elsenullend)物理
fromtb
groupby姓名

--SQLSERVER2000动态SQL,指课程不止语文、数学、物理这三门课程。(以下同)
declare@sqlvarchar(8000)
set@sql='select姓名'
select@sql=@sql+',max(case课程when'''+课程+'''then分数else0end)['+课程+']'
from(selectdistinct课程fromtb)asa
set@sql=@sql+'fromtbgroupby姓名'
exec(@sql)

--SQLSERVER2005静态SQL。
select*from(select*fromtb)apivot(max(分数)for课程in(语文,数学,物理))b

--SQLSERVER2005动态SQL。
declare@sqlvarchar(8000)
select@sql=isnull(@sql+'],[','')+课程fromtbgroupby课程
set@sql='['+@sql+']'
exec('select*from(select*fromtb)apivot(max(分数)for课程in('+@sql+'))b')

希望对你的学习有帮助。

‘捌’ 如何运用SQL语句,在表中某个字段前统一加前缀,并保证字段长度相同

update
表名
set
字段名
=right(cast('000000'
as
nvarchar)
+cast(字段名
as
nvarchar),6)
需要使用cast函数将前缀的几个0和字段里的值转换成字符串类型,否则会进行数值型的数学相加,而不是将两个字符串连接到一起