當前位置:首頁 » 編程語言 » sql行列轉換動態

sql行列轉換動態

發布時間: 2023-06-09 23:55:09

sql server 行轉列

創建測試表

createtabletest
(_keyvarchar(10),
_valuevarchar(10),
idint)

insertintotestvalues('ceshi','測試值',10)
insertintotestvalues('ceshi','測試值',11)
insertintotestvalues('ceshi2','測試值2',11)

執行

declare@sqlvarchar(4000)
set@sql='selectid'
select@sql=@sql+',max(case_keywhen'''+[_key]+'''then[_value]end)as
['+_key+']'
from(selectdistinct_keyfromtest)asa
select@sql=@sql+'fromtestgroupbyid'
exec(@sql)

結果


額,我那個第三條數據id寫錯了,不過方法還是這個

⑵ sql 2000 結合兩張表「動態」實現 行轉列

select
bdate,
max(case when area='保養一區' then 1 else 0 end),
max(case when area='保養二區' then 1 else 0 end),
max(case when area='保養三區' then 1 else 0 end),
……
from tablename
group by bdate

熱點內容
和平精英安卓版怎麼調成中文 發布:2025-02-13 22:16:10 瀏覽:36
混沌起源需要什麼配置玩 發布:2025-02-13 22:14:33 瀏覽:277
vs引入文件夾 發布:2025-02-13 22:05:52 瀏覽:127
安卓手機如何調無限電量 發布:2025-02-13 22:05:50 瀏覽:113
phppost長度 發布:2025-02-13 22:05:45 瀏覽:626
烈火戰神源碼 發布:2025-02-13 22:04:51 瀏覽:619
源碼貓交易 發布:2025-02-13 22:04:49 瀏覽:756
廣告腳本鏡頭 發布:2025-02-13 22:04:48 瀏覽:986
安卓手機哪個app自拍更真實 發布:2025-02-13 22:00:19 瀏覽:194
安卓系統密碼忘了怎麼辦 發布:2025-02-13 21:49:28 瀏覽:972