sql - 在 SQL Server 中将值转换为列
问题描述
我有这个专栏:
floor
---------
dep:first
dep:second
dep:third
dep:fourth
我想让它像这样:
DEP
------
first
second
third
fourth
我正在尝试这个:
select
max(case when floor = 'dep:first' then 'first' end) DEP,
max(case when floor = 'dep:second' then 'second' end) DEP,
max(case when floor = 'dep:third' then 'third' end) DEP,
max(case when floor = 'dep:fourth' then 'fourth' end) DEP,
from
db.table
但结果返回 DEP 列 4 次:
DEP |DEP |DEP |DEP |
-------|-------|--------|-------|
first |second |third |fourth |
解决方案
使用replace()
orstuff()
怎么样?
select replace(floor, 'dep:', '')
或者:
select stuff(floor, 1, 4, '')
推荐阅读
- firebase - Firebase 存储安全?
- reactjs - 在 React Native 中垂直对齐组件
- html - 如何为两个 div 的一半提供背景颜色?
- c - devc++中,为什么只能输入数字,却不能求和?
- android - Android TV 中的谷歌助手和应用操作问题
- spring - 不要使用 RestTemplate 向服务发出相同的 http 请求,而是在不使用数据库的情况下将响应缓存在某处?
- flutter - 应该只有一项具有 [DropdownButton] 的值
- angular - Angular 应用程序在完全加载之前显示屏幕空白
- google-cloud-platform - 将 Google 托管的 ssl 证书与 dockerized uwsgi 应用程序一起使用
- css - 带有 flexbox 的 CSS 定位元素