Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivoting in Sybase SQL Query?

Tags:

sql

pivot

sybase

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

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?

like image 520
Borophyll Avatar asked Nov 13 '11 19:11

Borophyll


People also ask

How do you PIVOT in SQL query?

Introduction to SQL Server PIVOT operator You follow these steps to make a query a pivot table: First, select a base dataset for pivoting. Second, create a temporary result by using a derived table or common table expression (CTE) Third, apply the PIVOT operator.

Can you PIVOT in Exasol?

Exasol does not support the PIVOT function, but this example shows how it can be expressed in Exasol anyway. Exasol does not support the UNPIVOT function, but this example shows how it can be expressed in Exasol anyway. Both, Oracle and Exasol, support analytic functions. See Analytic Functions for more details.

Can you create a pivot table in SQL?

We do this by clicking on the Pivot tab in the SQL Spreads Designer and then specifying the column we want to pivot and the column which has the values in it. The data in Excel is now pivoted.


1 Answers

The classic way to pivot to a fixed number of columns is like this:

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
like image 191
Lord Peter Avatar answered Sep 18 '22 00:09

Lord Peter