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

時間 2022-03-12 18:30:04

1樓:匿名使用者

假如你的 ele 中的元素, 數量是已知的參考:行列轉換

假如你的 ele 中的元素, 數量是未知的, 可能刪除,可能新增,可能修改。

參考;動態行列轉換處理

2樓:匿名使用者

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 列名

3樓:

select id,

(select si from a as b where a,id = b.id) as si,

(select mn from a as b where a,id = b.id) as mn,

(select c from a as b where a,id = b.id) as c,

(select s from a as b where a,id = b.id) as s

from a as a

4樓:謇霜

case when then就能實現了。

5樓:

自己寫了一個僅供參考

create table #a

(d varchar(4),

ele varchar(4),

evalue float

)insert into #a values(011,'c',0.50)

insert into #a values(011,'si',0.21)

insert into #a values(011,'mn',0.25)

insert into #a values(011,'s',0.25)

insert into #a values(012,'c',0.24)

insert into #a values(012,'mn',0.30)

insert into #a values(012,'s',0.22)

select d,max(case ele when 'c' then evalue else null end) c,

max(case ele when 'si' then evalue else null end) si,

max(case ele when 'mn' then evalue else null end) mn,

max(case ele when 's' then evalue else null end) s

from #a group by d

drop table #a

mysql中查詢(資料庫中的)縱向轉(查詢結果顯示為)橫向.

6樓:匿名使用者

1、在mysql環境,建立資料庫表,

create table test_data1(id int, name varchar(20), day varchar(20))

2、插入測試資料,

insert into test_data1 values(1,'liu',1);

insert into test_data1 values(2,'liu',3);

insert into test_data1 values(3,'wang',1);

insert into test_data1 values(4,'wang',2);

insert into test_data1 values(4,'wang',4);

3、查詢表中資料,select * from test_data1;

4、編寫目標sql;

select name,

max(case when day=1 then 1 end) d_1,

max(case when day=2 then 2 end) d_2,

max(case when day=3 then 3 end) d_3,

max(case when day=4 then 4 end) d_4

from test_data1 t group by name

7樓:知道小爺

mysql中,查詢縱向轉橫向可用case when語句。

工具:mysql 5.6

步驟:1、student表中有如下資料:

2、縱向顯示每個班級的總分,用如下語句:

select class,sum(score) from student group by class;

查詢結果:

3、要將結果橫向顯示,則用case when語句,語句如下:

select

sum(case when class='一年一班' then score else 0 end) 一年一班成績,

sum(case when class='一年二班' then score else 0 end) 一年二班成績,

sum(case when class='一年三班' then score else 0 end) 一年三班成績

from student;

查詢結果:

8樓:

sql語句後加"/g",比如「select * from table/g」

9樓:匿名使用者

在輸出的時候迴圈控制,應該可以實現想要的任何效果。

sql語句怎麼將縱向排列的資料轉換成橫向排列

10樓:

語句:select 姓名 , case when 日期 = '2016-5-2' then 班次 else '' end [2016-5-2]

, case when 日期 = '2016-5-3' then 班次 else '' end [2016-5-3]

from table

怎麼將資料庫中橫向顯示的直,用sql語句使它縱向顯示?

11樓:匿名使用者

select 'enkey' enkey,

cast(sum(case when t.enkey = 'a' then 1 else 0 end) as varchar) a,

cast(sum(case when t.enkey = 'b' then 2 else 0 end) as varchar) b,

cast(sum(case when t.enkey = 'c' then 3 else 0 end) as varchar) c

from (select enkey,listkey from indexlists where enkey in('m','a','xs','ys','tz') ) t

union all

select 'listkey','a','b','c'

sql中把查詢結果從豎列轉為橫向

12樓:雨中花雨落

select a as a,max(case when b='a' then c end) as a,max(case when b='b' then c end) as b,max(case when b='c' then c end) as c,max(case when b='d' then c end) as d from a

SQL語句多表查詢,SQL同時查詢多個表

1 開啟microsoft sql server 2012,選中需要查詢所有表的資料庫。3 點選 新建查詢 後,會在右邊彈出一個框,我們需要在這裡編寫sql語句,來查詢該資料庫下的所有表結構。4 編寫sql語句,點選 執行 當然,這表語句我們可以根據實際情況,來改變條件只查詢需要的表名。5 這時,會...

sql表關聯,SQL語句 關聯表的問題

我覺得,應該加限定語句 where 比如 4 刪除資料 delete from table name where conditions 說明 刪除符合條件的資料。說明 關於where條件後面如果包含有日期的比較,不同資料庫有 不同的表示式。具體如下 1 如果是access資料庫,則為 where m...

SQL複雜表查詢

看了頭暈,為何要用中文欄位呀,呵呵 select 主表.工序表.工序名稱,作廢表.作廢名稱 from 主表 left 工序表 on 主表.工序代號 工序表.工序代號left 作廢表 on 主表.作廢代號 作廢表.作廢代號where 主表.工序代號 in 601,602 你要查的錯誤工序代號,用逗號隔...