❶ 求大神一sql語句,怎麼將下面的錶行轉列,按列綁定,在線等!求紅領巾幫忙
--行列轉換(列長度不大於255,行過多則無法進行轉換與資料庫機制相關)
create procere TransTable @TableName nvarchar(255)
as
declare @FlagNo int,@Rows int
create table ##tmp_table(ColName nvarchar(255))
declare @sql nvarchar(4000)
--以原來的列標題作為第一列數據
set @sql='insert into ##tmp_table
select name from syscolumns where id=(select max(id) from sysobjects where xtype=''u'' and name='''+@TableName+''')
'
exec sp_executesql @sql
set @FlagNo=1
set @sql='select @Count=COUNT(*) from '+@TableName
exec sp_executesql @sql,N'@Count int OutPut',@Rows Output
if ISNULL(@Rows,0)=0
begin
select * from ##tmp_table
drop table ##tmp_table
return
end
--列建立
set @FlagNo=1
while @FlagNo<=@Rows
begin
set @sql='
alter table ##tmp_table
add FColumn'+CAST(@FlagNo as nvarchar(10))+' nvarchar(255)'
exec sp_executesql @sql
set @FlagNo=@FlagNo+1
end
declare @ColName nvarchar(255)
declare cur cursor for
select ColName from ##tmp_table
open cur
fetch next from cur into @ColName
while @@FETCH_STATUS=0
begin
set @sql='
declare @FValue nvarchar(255)
declare @curFlag int
declare cur_value cursor for
select cast('+@ColName+' as nvarchar(255)) from '+@TableName+'
set @curFlag=1
open cur_value
fetch next from cur_value into @FValue
while @@FETCH_STATUS=0
begin
if @curFlag<='+CAST(@Rows as nvarchar(20))+'
exec TransTableUpdateValue '''+@ColName+''',@curFlag,@FValue
fetch next from cur_value into @FValue
set @curFlag=@curFlag+1
end
close cur_value
DEALLOCATE cur_value
'
--select @sql
exec sp_executesql @sql
fetch next from cur into @ColName
end
close cur
DEALLOCATE cur
select * from ##tmp_table
drop table ##tmp_table
--更新單元數據
create procere TransTableUpdateValue @ColName nvarchar(255),@FlagInt int,@value nvarchar(255)
as
declare @Sql nvarchar(1000)
set @Sql='
update ##tmp_table set FColumn'+CAST(@FlagInt as nvarchar(10))
+'='''+isnull(@value,'')+''' where ColName='''+@ColName+''''
--select @sql
exec sp_executesql @sql
execTransTable'你的表'--(行不能太多,因為每個列定義長度255,過長無法使用,sql,你知道的)
❷ 資料庫 sql 行列轉換是這樣的嗎
sql行列轉換代碼:
create (keyId int identity,---自動流水號
nian int not null, ---年份
info1 varchar(30),
info2 varchar(30),
qty int ---數量
)
insert into A (nian,info1,info2,qty)values(2012,'A','B',2)
insert into A (nian,info1,info2,qty)values(2013,'B','C',3)
insert into A (nian,info1,info2,qty)values(2008,'C','D',4)
insert into A (nian,info1,info2,qty)values(2018,'D','E',5)
insert into A (nian,info1,info2,qty)values(2012,'A','F',6)
insert into A (nian,info1,info2,qty)values(2008,'B','G',7)
insert into A (nian,info1,info2,qty)values(2012,'C','H',12)
insert into A (nian,info1,info2,qty)values(2013,'D','I',16)
insert into A (nian,info1,info2,qty)values(2013,'A','J',20)
go
--行轉列方法1:CASE WHEN,兼容sql2000
select info1,
sum(case when nian=2012 then qty end) as [2012],
sum(case when nian=2008 then qty end) as [2008],
sum(case when nian=2018 then qty end) as [2018],
sum(case when nian=2013 then qty end) as [2013]
from A
group by info1;
GO
--行轉列方法2:PIVOT,sql2005及以後版本
select *
from (select info1,nian,qty from A) as infotest
pivot(sum(qty) for nian in([2012],[2008],[2018],[2013]))as lie
GO
truncate table A
drop table A
如有疑問,及時溝通!
❸ Oracle 資料庫行轉列
createtableA(名稱varchar2(10),"1月"varchar2(10),"2月"varchar2(10),"3月"varchar2(10),"4月"varchar2(10))
insertintoAvalues('土豆',50,60,70,80);
insertintoAvalues('白菜',150,160,170,180);
commit;
select*fromA;
select*from(
select名稱,'1月'月份,"1月"值fromA
unionall
select名稱,'2月'月份,"2月"值fromA
unionall
select名稱,'3月'月份,"3月"值fromA
unionall
select名稱,'4月'月份,"4月"值fromA)
where名稱='土豆'
結果:
❹ 如何將excel表格行轉列
以WPS 2019版本為例:
關於WPS表格轉置,您可使用WPS參考下述步驟完成操作:
先復制內容,然後在需要橫排顯示的地方點擊滑鼠右鍵,選擇「選擇性粘貼」勾選「轉置」確定。
❺ 資料庫行轉列
我之前也想將列轉行,嘗試了很多方法,後來覺得插入數據透視表最簡單。將表導出後用插入數據表的方法可以實現你想要的結果。
❻ excel中怎樣批量將行數據轉換成列數據
你好,樓主想要的功能,可以使用VBA程序實現。
(一)VBA程序代碼如下:(代碼運行之前請做好數據備份,以免丟失)
Subabcd()
Dimi1,i2,i3,str
OnErrorResumeNext
Setmysheet1=ThisWorkbook.Worksheets("Sheet1")'定義工作表Sheet1
Fori1=1To1000'從第1行到1000行
Ifmysheet1.Cells(i1,1)<>""Then'如果單元格不是空白,則
str=Mid(mysheet1.Cells(i1,1),1,1)'截取第一個字元
IfIsNumeric(str)=TrueThen'如果截取的字元是數字,則
i2=i1
i3=1
Else
i3=i3+1
mysheet1.Cells(i2,i3)=mysheet1.Cells(i1,1)'把ABCD項寫入含有數字的那一行
EndIf
EndIf
Next
EndSub
❼ SqlServer資料庫怎麼實現行轉列的sql語句
PIVOT 用於將列值旋轉為列名(即行轉列),在 SQL Server 2000可以用聚合函數配合CASE語句實現
PIVOT 的一般語法是:PIVOT(聚合函數(列) FOR 列 in (…) )AS P
注意:PIVOT、UNPIVOT是SQL Server 2005 的語法,使用需修改資料庫兼容級別(在資料庫屬性->選項->兼容級別改為 90 )
SQL2008 中可以直接使用
完整語法:
table_source
PIVOT(
聚合函數(value_column)
FOR pivot_column
IN(<column_list>)
)
View Code
UNPIVOT 用於將列明轉為列值(即列轉行),在SQL Server 2000可以用UNION來實現
完整語法:
table_source
UNPIVOT(
value_column
FOR pivot_column
IN(<column_list>)
)
❽ sql怎樣將行的值變為列,,,,
方法/步驟
首先我們建立一張表,名為RToC,各個欄位的設計如下圖,分別是name,course,score,表示姓名,成績與分數,如圖所示。
通過以上幾個步驟,我們就可以輕松的實現行列轉換了。同樣,我們如果要把列轉換成行, 應該怎麼做呢?同樣我們可以採用unpivot函數輕松實現。