我正在寻找一种方法来调整以下结果......
ID | Group_Level | Group_Values
1 | Division | Value 1
2 | Department | Value 2
3 | Class | Value 3
Run Code Online (Sandbox Code Playgroud)
进入以下结构....
ID | Division | Department | Class
1 | Value 1 | Value 2 | Value 3
2 | Value 1 | Value 2 | Value 3
Run Code Online (Sandbox Code Playgroud)
列数是固定的(它将始终为division/department/class).该查询适用于Sybase ...已经无法弄清楚如何实现这种旋转.有什么建议?
Lor*_*ter 11
转向固定数量列的经典方法如下:
select id,
max (case when group_level = 'Division' then Group_Values else null end) Division,
max (case when group_level = 'Department' then Group_Values else null end) Department,
max (case when group_level = 'Class' then Group_Values else null end) Class
from
YourTable
group by id
Run Code Online (Sandbox Code Playgroud)
您需要一些键来定义 3 行集。然后,您只需自行加入
所以对于这样的数据...
ID | GroupID | Group_Level | Group_Values
1 | 1 | Division | Value 1
2 | 1 | Department | Value 2
3 | 1 | Class | Value 3
4 | 2 | Division | Value 1
5 | 2 | Department | Value 2
6 | 2 | Class | Value 3
Run Code Online (Sandbox Code Playgroud)
你会有
SELECT
Div.GroupID, Div.Group_Values, Dept.Group_Values, Cl.Group_Values
FROM
MyTable Div
JOIN
MyTable Dept ON Div.GroupID = Dept.GroupID
JOIN
MyTable Cl ON Div.GroupID = Cl.GroupID
WHERE
Div.Group_Level = 'Division'
AND
Dept.Group_Level = 'Department'
AND
Cl.Group_Level = 'Class'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16813 次 |
| 最近记录: |