⑴ sql豎表數據批量插入到橫表中 注意是要插入到表中,不單單是查詢出來
declare @idx int,@colName varchar(50),@tableID int,@sql varhcar(2000)
select @tableID=id from sysobjects where name='橫表名'
set @idx=1
set @colName='Marks'+convert(varchar,@idx)
while(if exists(select * from sysocolumns where id=@tableID and name=@colName))
begin
set sql='insert into 縱表 select stuno,stuname,『+@colName+』from 橫表'
exec(@sql)
set @idx=@idx+1
set @colName='Marks'+convert(varchar,@idx)
end
沒有調試,直觀的基本上就是這個寫法,復雜點的你可以單獨再建個要需讀取的列名表,循環取列名拼SQL再執行。
或者再寫的復雜點,循環用union拼接select的部分
以上是針對類似問題的通用方案,適合列很多的情況。
如果僅限於上邊的例 子不做任何擴展,最死的寫法就是
insert into 縱表
from select * from(select stuno,stuname,marks1 from 橫表
union select stuno,stuname,marks2 from 橫表
union select stuno,stuname,marks3 from 橫表
)t
當然如果直接查詢生成表,也可以用select ...into的寫法
⑵ mysql 縱表轉橫表,高手請支招
你這個需要後台處理一下才可以,直接轉換比較麻煩,效率也不高。因為你需要計算出每天的開始時間和結束時間insert into newtable select employeeID,days,MAX(cardTime) as endtime,Min(cardTime) as starttime from
table group by employeeID,days; 大概就這意思,自己在調試調試吧
⑶ SQL 如何查詢時 豎著的數據 橫著顯示
你這個是將縱表轉換為橫表,例如如下數據:
'wangming', 'shuxue', 100
'wangming', 'yuwen', 90
'wangming', 'yingyu', 140
可以使用如下語句處理:
select a.v_name,a.v_score shuxue,b.v_score yuwen,c.v_score yingyu from temp_1 a,temp_1 b,temp_1 c
where a.v_name=b.v_name
and a.v_name=c.v_name
and a.v_name='wangming'
and a.v_course='shuxue'
and b.v_course='yuwen'
and c.v_course='yingyu';
⑷ sql怎麼將縱向數據列表轉化為橫向數據欄位
--行列互轉
/******************************************************************************************************************************************************
以學生成績為例子,比較形象易懂
整理人:中國風(Roy)
日期:2008.06.06
******************************************************************************************************************************************************/
--1、行互列
--> --> (Roy)生成測試數據
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] 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([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')
生成靜態:
select
[Student],
[數學]=max(case when [Course]='數學' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英語]=max(case when [Course]='英語' then [Score] else 0 end),
[語文]=max(case when [Course]='語文' then [Score] else 0 end)
from
Class
group by [Student]
GO
動態:
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')
生成靜態:
select *
from
Class
pivot
(max([Score]) for [Course] in([數學],[物理],[英語],[語文]))b
生成格式:
/*
Student 數學 物理 英語 語文
------- ----------- ----------- ----------- -----------
李四 77 85 65 65
張三 87 90 82 78
(2 行受影響)
*/
------------------------------------------------------------------------------------------
go
--加上總成績(學科平均分)
--2000方法:
動態:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+',[總成績]=sum([Score]) from Class group by [Student]')--加多一列(學科平均分用avg([Score]))
生成動態:
select
[Student],
[數學]=max(case when [Course]='數學' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英語]=max(case when [Course]='英語' then [Score] else 0 end),
[語文]=max(case when [Course]='語文' then [Score] else 0 end),
[總成績]=sum([Score]) --加多一列(學科平均分用avg([Score]))
from
Class
group by [Student]
go
--2005方法:
動態:
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字元串@s中第一個逗號
exec('select [Student],'+@s+',[總成績] from (select *,[總成績]=sum([Score])over(partition by [Student]) from Class) a
pivot (max([Score]) for [Course] in('+@s+'))b ')
生成靜態:
select
[Student],[數學],[物理],[英語],[語文],[總成績]
from
(select *,[總成績]=sum([Score])over(partition by [Student]) from Class) a --平均分時用avg([Score])
pivot
(max([Score]) for [Course] 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],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字元串@s中第一個union all
+',[Score]='+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],[Course]')--增加一個排序
生成靜態:
select *
from (select [Student],[Course]='數學',[Score]=[數學] from Class union all
select [Student],[Course]='物理',[Score]=[物理] from Class union all
select [Student],[Course]='英語',[Score]=[英語] from Class union all
select [Student],[Course]='語文',[Score]=[語文] from Class)t
order by [Student],[Course]
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,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')
go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([數學],[物理],[英語],[語文]))b
生成格式:
/*
Student Course Score
------- ------- -----------
李四 數學 77
李四 物理 85
李四 英語 65
李四 語文 65
張三 數學 87
張三 物理 90
張三 英語 82
張三 語文 78
(8 行受影響)
*/
⑸ sql 裡面怎麼把豎表變成橫表
select 姓名,sum(case when 科目='數學' then 分數 end) as 數學,sum(case when 科目='語文' then 分數 end) as 語文,sum(case when 科目='英語' then 分數 end) as 英語 from 表名
group by 姓名
⑹ oracle sql 中 如何實現table的行列轉換
你所謂的行列轉換應該是指縱表轉橫表,橫表轉縱表.
給你個例子
縱表轉橫表:
使用DECODE語句,可以很方便的將縱表轉為橫表,例子如下:
原表查詢結果:
ID MAJOR CURRENT_CREDITS
------ ---------------- ---------------
10000 Computer Science 98
10000 History 88
10001 Computer Science 75
10000 Economics 66
我們要把各科成績從同一列轉到不同的列中去。
SQL>?select id,
sum(decode(major,』Computer Science』,current_credits,0)) cs,
sum(decode(major,』History』,current_credits,0)) his,
sum(decode(major,』Economics』,current_credits,0)) eco
from students
group by id
ID CS HIS ECO
------ ----------- ------------- -----
10000 98 88 66
10001 75
橫表轉縱表:
使用 UNION 即可實現將橫表轉為縱表,以上面的表為例:
轉換前:
ID CS HIS ECO
------ ----------- ------------- -----
10000 98 88 66
SQL>?select id, 』Computer Science』 major,cs current_credits
from students
union
select id, 』History』 major,his current_credits
from students
union
select id, 』Economics』 major,eco current_credits
from students
ID MAJOR CURRENT_CREDITS
------ ---------------- ---------------
10000 Computer Science 98
10000 History 88
10000 Economics 66
⑺ 如何通過sql將表中豎向數據轉換成橫向數據
行列轉換等經典SQL語句
參考資料:http://blog.csdn.net/kiki113/archive/2009/04/24/4105929.aspx
1.--行列轉換
原表: 姓名 科目 成績
張三 語文 80
張三 數學 90
張三 物理 85
李四 語文 85
李四 物理 82
李四 英語 90
李四 政治 70
王五 英語 90
轉換後的表: 姓名 數學 物理 英語 語文 政治
李四 0 82 90 85 70
王五 0 0 90 0 0
張三 90 85 0 80 0
實例:
create table cj --創建表cj
(
ID Int IDENTITY (1,1) not null, --創建列ID,並且每次新增一條記錄就會加1
Name Varchar(50),
Subject Varchar(50),
Result Int,
primary key (ID) --定義ID為表cj的主鍵
);
--Truncate table cj
--Select * from cj
Insert into cj
Select '張三','語文',80 union all
Select '張三','數學',90 union all
Select '張三','物理',85 union all
Select '李四','語文',85 union all
Select '李四','物理',82 union all
Select '李四','英語',90 union all
Select '李四','政治',70 union all
Select '王五','英語',90
--行列轉換
Declare @sql varchar(8000)
Set @sql = 'Select Name as 姓名'
Select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result else 0 end) ['+Subject+']'
from (select distinct Subject from cj) as cj --把所有唯一的科目的名稱都列舉出來
Select @sql = @sql+' from cj group by name'
Exec (@sql)
2. 行列轉換--合並
原表: 班級 學號
1 1
1 2
1 3
2 1
2 2
3 1
轉換後的表: 班級 學號
1 1,2,3
2 1,2
3 1
實例:
Create table ClassNo --創建表ClassNo
(
ID Int IDENTITY(1,1) not null, --創建列ID,並且每次新增一條記錄就會加1
Class Varchar(50), --班級列
Number Varchar(50), --學號列
Primary Key(ID) --定義ID為表ClassNo的主鍵
);
--Truncate Table ClassNo
--Select * from ClassNo
Insert Into ClassNo
Select 1,1 Union all
Select 1,2 Union all
Select 1,3 Union all
Select 2,1 Union all
Select 2,2 Union all
Select 3,1
創建一個合並的函數
--Drop Function KFReturn
Create Function KFReturn(@Class Varchar(50))
Returns Varchar(8000)
as
Begin
Declare @str Varchar(8000)
Set @str = ''
Select @str = @str + cast(Number as Varchar(50)) + ',' from ClassNo Where Class = @Class
Set @str = SubString(@str,1,len(@str)-1)
Return(@str)
End
--調用自定義函數得到結果
Select Distinct Class,dbo.KFReturn(Class) From ClassNo
3:列轉行
--Drop Table ColumnToRow
Create table ColumnToRow
(
ID Int IDENTITY(1,1) not null, --創建列ID,並且每次新增一條記錄就會加1
a int,
b int,
c int,
d int,
e int,
f int,
g int,
h int,
Primary Key(ID) --定義ID為表ColumnToRow的主鍵
);
--Truncate Table ColumnToRow
--Select * from ColumnToRow
Insert Into ColumnToRow
Select 15,9,1,0,1,2,4,2 Union all
Select 22,34,44,5,6,7,8,7 Union all
Select 33,44,55,66,77,88,99,12
Declare @sql Varchar(8000)
Set @sql = ''
Select @sql = @sql + rtrim(name) + ' from ColumnToRow union all Select ' from SysColumns Where id = object_id('ColumnToRow')
Set @sql = SubString(@sql,1,len(@sql)-70)
--70的長度就是這個字元串'from ColumnToRow union all Select ID from ColumnToRow union all Select ',因為它會把ID這一列的值也算進去,所以要把它截掉
Exec ('Select ' + @sql + ' from ColumnToRow')
⑻ sql如何自動實現更新,豎表變橫表,是要用觸發器還是別的方法實現,具體代碼如何寫
先給你講一下你說的橫表變豎表,其實就是行列轉換,我寫個例子給你看看:
列轉行
錄入經營范圍時候會遇到列傳行的問題解決方案如下:
在temp1 表有一下欄位內容:
⑼ sql server 縱表轉橫表
如果單純是你如上數據的話這樣:
創建表:
createtablet
(idint,
orderidint,
prfnoint,
prfidvarchar(10));
insertintotvalues(16385,171202,1,'FB065_1');
insertintotvalues(16385,171202,2,'FB065_06');
insertintotvalues(16385,171202,3,'FB065_06');
insertintotvalues(16385,171202,4,'FB065_06');
insertintotvalues(16386,171202,1,'FB065_1');
insertintotvalues(16386,171202,2,'FB065_06');
insertintotvalues(16386,171202,3,'FB065_06');
insertintotvalues(16386,171202,4,'FB065_06');
執行:
selectid,orderid,
max(casewhenprfno=1thenprfidend)prfid1,
max(casewhenprfno=2thenprfidend)prfid2,
max(casewhenprfno=3thenprfidend)prfid3,
max(casewhenprfno=4thenprfidend)prfid4
fromtgroupbyid,orderid
結果:
如果需求復雜的話,就要改別的方法了。
⑽ mysql縱表轉橫表
分兩次進行不同的查詢就可以實現了
先查出組員信息,比如組員數量、文章總數、評論總數等
再查出組長信息
兩個查詢一合並就可以了
SQL角本如下:
selecta.用戶名組長,b.組員數量,b.文章總數,b.評論總數
from(select編號,用戶名fromtable_namewhere角色='組長')a,--找組長信息
(select組長,
count(1)組員數量,
sum(文章)文章總數,
sum(評論)評論總數
fromtable_name
where組長isnotnull
groupby組長)b--找組員信息
wherea.編號=b.組長