透视Sybase SQL查询? [英] Pivoting in Sybase SQL Query?

查看:71
本文介绍了透视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

进入以下结构....

Into the following structure....

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查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆