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

sql查詢縱表成橫表

發布時間: 2022-06-08 14:47:50

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.所在位置

❷ 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豎表數據批量插入到橫表中 注意是要插入到表中,不單單是查詢出來

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縱表轉橫表

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

  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.組長

❺ sql如何自動實現更新,豎表變橫表,是要用觸發器還是別的方法實現,具體代碼如何寫

先給你講一下你說的橫表變豎表,其實就是行列轉換,我寫個例子給你看看:

列轉行

錄入經營范圍時候會遇到列傳行的問題解決方案如下:

在temp1 表有一下欄位內容:

❻ 求一個SQL查詢語句,將縱向表橫向表示

select id,max(case ele when 'c' then ecalue else null end) as c,
max(case ele when 'si' then ecalue else null end) as si,
max(case ele when 'mn' then ecalue else null end) as mn,
max(case ele when 's' then ecalue else null end) as s,
……from 表 group by id
--有多少列就加max(case ele when 列名 then ecalue else null end) as 列名

❼ 如何通過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 裡面怎麼把豎表變成橫表

select 姓名,sum(case when 科目='數學' then 分數 end) as 數學,sum(case when 科目='語文' then 分數 end) as 語文,sum(case when 科目='英語' then 分數 end) as 英語 from 表名
group by 姓名

❾ 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

結果:

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