當前位置:首頁 » 編程語言 » 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

熱點內容
php取兩位小數點 發布:2025-09-16 15:12:40 瀏覽:310
加密塊流加密 發布:2025-09-16 15:07:36 瀏覽:695
sqldeveloper導出表 發布:2025-09-16 15:07:33 瀏覽:361
xbox360ftp 發布:2025-09-16 14:45:34 瀏覽:847
火車站附近wifi密碼是多少 發布:2025-09-16 14:45:30 瀏覽:190
國家標准加密 發布:2025-09-16 14:45:27 瀏覽:949
php集成支付寶 發布:2025-09-16 14:05:28 瀏覽:718
php的pregmatchall 發布:2025-09-16 14:04:43 瀏覽:437
片上緩存區 發布:2025-09-16 13:49:18 瀏覽:153
查驗技巧腳本 發布:2025-09-16 13:41:59 瀏覽:727