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

資料庫行列轉換

發布時間: 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語法就不說了,網上一堆一堆的 ~

熱點內容
des解密java 發布:2025-07-16 14:26:45 瀏覽:796
sql2000新建資料庫 發布:2025-07-16 14:19:47 瀏覽:941
硬碟錄像機用nas存儲 發布:2025-07-16 14:16:55 瀏覽:911
南寧ip伺服器租用 發布:2025-07-16 14:11:30 瀏覽:505
androidcategory 發布:2025-07-16 14:11:25 瀏覽:214
三千多電腦哪個品牌配置好 發布:2025-07-16 14:11:20 瀏覽:324
馬里奧源碼 發布:2025-07-16 14:01:58 瀏覽:392
我的世界伺服器娘 發布:2025-07-16 14:01:08 瀏覽:116
連接雲伺服器閃屏 發布:2025-07-16 13:59:03 瀏覽:937
無油空壓機怎麼配置 發布:2025-07-16 13:53:25 瀏覽:976