當前位置:首頁 » 編程語言 » sql成績縱表轉橫表
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql成績縱表轉橫表

發布時間: 2022-05-31 21:48:22

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

結果:

如果需求復雜的話,就要改別的方法了。

資料庫縱表變橫表會不會影響性能

橫表就是普通的建表方式,如一個表結構為:主鍵、欄位1、欄位2、欄位3。。。 如果變成縱表後,則表結構為: 主鍵、欄位代碼、欄位值。而欄位代碼則為欄位1、欄位2、欄位3。 具體為電信行業的例子。以用戶帳單表為例一般出賬時用戶有很多費用客戶,其數據一般存儲為:時間,客戶ID,費用科目,費用。這種存儲結構一般稱為縱表,其特點是行數多,欄位少。縱表在使用時由於行數多,統計用戶數或對用戶進行分檔時還需要進行GROUP BY 操作,性能低,且操作不便,為提高性能,通常根據需要將縱表進行匯總,形成橫表,比如:時間、客戶ID,基本通話費、漫遊通話費,國內長途費、國際長途費....。通常形成一個客戶一行的表,這種表統計用戶數或做分檔統計時比較方便。另外,數據挖掘時用到的寬表一般也要求是橫表結構。縱表對從資料庫到內存的映射效率是有影響的,但細一點說也要一分為二:縱表的初始映射要慢一些;縱表的變更的映射可能要快一些,如果只是改變了單個欄位時,畢竟橫表欄位比縱表要多很多

⑶ 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';

⑷ mysql縱表轉橫表

分兩次進行不同的查詢就可以實現了

  1. 先查出組員信息,比如組員數量、文章總數、評論總數等

  2. 再查出組長信息

  3. 兩個查詢一合並就可以了

SQL角本如下:

selecta.用戶名組長,b.組員數量,b.文章總數,b.評論總數
from(select編號,用戶名fromtable_namewhere角色='組長')a,--找組長信息
(select組長,
count(1)組員數量,
sum(文章)文章總數,
sum(評論)評論總數
fromtable_name
where組長isnotnull
groupby組長)b--找組員信息
wherea.編號=b.組長

⑸ mysql 縱表轉橫表,高手請支招

你這個需要後台處理一下才可以,直接轉換比較麻煩,效率也不高。因為你需要計算出每天的開始時間和結束時間insert into newtable select employeeID,days,MAX(cardTime) as endtime,Min(cardTime) as starttime from
table group by employeeID,days; 大概就這意思,自己在調試調試吧

⑹ 急求:SQL語句進行行列轉換!!!

你的這種變化貌似是毫無意義的,或許
你想要的變換時下面的這種情況吧。
******原來的縱表tb*****
學號
科目
成績
1001
語文
90
1001
數學
80
1001
英語
70
1002
語文
95
1002
數學
85
1002
英語
75
*****目標的橫表*****
學號
語文
數學
英語
總分
1001
90
80
70
240
1002
95
85
75
255
sql語句:
select
學號
as
'學號',
sum(case
科目
when
'語文'
then
成績
else
0
end)
as
'語文',
sum(case
科目
when
'數學'
then
成績
else
0
end)
as
'數學',
sum(case
科目
when
'英語'
then
成績
else
0
end)
as
'英語',
sum(case
when
科目!=''
then
成績
end)
as
'總分'
from
tb
group
by
學號

⑺ SQL sever中將橫表轉縱表時的這段代碼是什麼意思,可以詳細解釋一下嗎

查詢每個 name 中 語文 的最大 result 值,如果 語文 的 result 沒有值,則返回0;
其他數學、物理原理一樣;

⑻ 資料庫sql 問題

select年份,sum(casewhen季度=1then產量end),sum(casewhen季度=2then產量end),sum(casewhen季度=3then產量end),sum(casewhen季度=4then產量end)from表名groupby年份orderby年份

⑼ 橫縱表的相互轉換!

--行列互轉
/******************************************************************************************************************************************************
以學生成績為例子,比較形象易懂

整理人:中國風(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 行受影響)
*/

⑽ SQL中的統計加橫表轉縱表

根本沒涉及橫縱。
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.所在位置