當前位置:首頁 » 編程語言 » 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-07-04 07:30:01 瀏覽:567
資料庫備機 發布:2025-07-04 07:30:00 瀏覽:530
靜態內部類java 發布:2025-07-04 07:25:45 瀏覽:232
玉林電信dns伺服器地址 發布:2025-07-04 07:17:34 瀏覽:437
用鏡像壓縮 發布:2025-07-04 07:17:31 瀏覽:635
lgg3如何設置鎖屏密碼 發布:2025-07-04 06:41:39 瀏覽:346
sqlserver2005命令 發布:2025-07-04 06:27:08 瀏覽:734
壓縮文件啥樣 發布:2025-07-04 06:21:42 瀏覽:300
linux的回車換行 發布:2025-07-04 06:21:39 瀏覽:134
c語言的窗口 發布:2025-07-04 06:20:54 瀏覽:985