當前位置:首頁 » 操作系統 » 資料庫行列轉換

資料庫行列轉換

發布時間: 2023-08-15 18:57:11

sql資料庫查詢,如何實現只顯示為某值的欄位

實現只顯示為某值的欄位,可以通過行列轉換實現。
以下是以sql server為例來說明:
select b.stu_name,
max(case a.subject when '語文' then a.grade else '' end) as 語文,
max(case a.subject when '數學' then a.grade else '' end) as 數學,
max(case a.subject when '英語' then a.grade else '' end) as 英語
from stu_grade a,stu_master b
where a.stu_no=b.stu_no
group by b.stu_name
資料庫為oralce的話執行
select b.stu_name,
max(case a.subject when '語文' then to_char(a.grade) else '' end) as 語文,
max(case a.subject when '數學' then to_char(a.grade) else '' end) as 數學,
max(case a.subject when '英語' then to_char(a.grade) else '' end) as 英語
from stu_grade a,stu_master b
where a.stu_no=b.stu_no
group by b.stu_name

⑵ 寫sql,怎麼將查詢結果的行列轉換呀

有意思的問題 給出一個參考的URL: 假設你表是這樣的結構部件 入庫日期 入庫數量 A 1/1 10 A 1/1 5 A 1/2 10 B 1/5 10 其實就是兩種方法,假設你的資料庫是11以前的,只能先定義好查哪天到哪天 然後那麼 selct 部件, sum( decode(入庫日期=1號,入庫數量,0), sum( decode(入庫日期=2號,入庫數量,0), 以此類推 from 入庫表 group BY 部件(原理上就是將不是這天的變成0,再合計)要是Oracle資料庫是11的話,就簡單了,直接pivot搞定 pivot語法就不說了,網上一堆一堆的 ~

熱點內容
伺服器名稱或ip從哪裡看 發布:2025-03-11 00:35:44 瀏覽:491
日本細菌戰資料庫 發布:2025-03-11 00:29:34 瀏覽:423
釣魚直播用什麼配置 發布:2025-03-11 00:28:39 瀏覽:415
高配置伺服器搭建虛擬機集群 發布:2025-03-11 00:27:18 瀏覽:369
在線印刷源碼 發布:2025-03-11 00:25:06 瀏覽:718
python矩陣轉置函數 發布:2025-03-11 00:22:53 瀏覽:547
java緩存策略 發布:2025-03-11 00:18:26 瀏覽:897
cc怎麼上傳視頻 發布:2025-03-11 00:17:51 瀏覽:805
安卓系統怎麼設置伴奏 發布:2025-03-11 00:16:13 瀏覽:183
shell腳本如何發送 發布:2025-03-11 00:14:37 瀏覽:606