當前位置:首頁 » 編程語言 » 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和欄位里的值轉換成字元串類型,否則會進行數值型的數學相加,而不是將兩個字元串連接到一起