Pivoting in Sybase SQL Query?(在 Sybase SQL 查询中进行透视?)
问题描述
我正在寻找一种方法来转换以下结果...
I am looking for a way to pivot the following results...
ID | Group_Level | Group_Values
1 | Division | Value 1
2 | Department | Value 2
3 | Class | Value 3
进入如下结构....
ID | Division | Department | Class
1 | Value 1 | Value 2 | Value 3
2 | Value 1 | Value 2 | Value 3
列数是固定的(始终是部门/部门/类).该查询是为 Sybase 设计的……目前还无法弄清楚如何实现这种旋转.有什么建议吗?
The number of columns is fixed (it will always be division/department/class). The query is intended for Sybase... have been unable to figure out how to achieve this sort of pivoting yet. Any advice?
推荐答案
您需要一些键来定义 3 行的集合.然后,你自己加入
You need some key to define the set of 3 rows. Then, you just self JOIN
所以对于这样的数据...
So for data like this...
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
你应该有
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'
这篇关于在 Sybase SQL 查询中进行透视?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Sybase SQL 查询中进行透视?
基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
