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

熱點內容
連接ftp異常中斷 發布:2025-09-16 12:52:33 瀏覽:287
3m移動辦公伺服器地址大全 發布:2025-09-16 12:22:35 瀏覽:256
什麼是直男的快樂密碼 發布:2025-09-16 12:22:32 瀏覽:910
修改資料庫存儲引擎 發布:2025-09-16 12:21:48 瀏覽:77
安徽調度伺服器品牌雲伺服器 發布:2025-09-16 12:02:17 瀏覽:777
資料庫表設計教程 發布:2025-09-16 10:50:47 瀏覽:348
朋友圈緩存如何清除 發布:2025-09-16 10:49:57 瀏覽:446
sqlserver數據類型 發布:2025-09-16 10:41:16 瀏覽:740
如何配置全站時間同步系統 發布:2025-09-16 10:19:13 瀏覽:177
java解析json文件 發布:2025-09-16 10:10:41 瀏覽:977