Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 view the result of a sql statement horizontally

Author  Topic 

MGA
Starting Member

28 Posts

Posted - 2010-09-28 : 05:57:18
How to view the result of a sql statement horizontally
if the result is :
C1 C2
A 12
B 15
C 45

i want it to be viewed in the following form and do this in the same sql statement that selects these columns
A B C
12 15 45

Sachin.Nand

2937 Posts

Posted - 2010-09-28 : 06:05:17
Read about the Pivot operator in BOL.

PBUH

Go to Top of Page

MGA
Starting Member

28 Posts

Posted - 2010-09-28 : 06:31:19
Ok
how to use the PIVOT in the select statement if there are relationships between the tables
Example:if i had these two tables

create table Employees(Empid int constraint eid primary key,Stuff varchar(50))
create table Salaries (Empid int constraint eid2 foregin key references Employees(Empid),Salary int)
the table data

Employees Salaries
Empid Stuff Empid salary
1 Manager 1 5000
2 technical 2 3000
3 developer 3 2000

how to write a sql statement with the PIVOT operator to select the data below
Manager technical developer
5000 3000 2000
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-09-28 : 06:36:16
Write a query to get a resultset like this

Empid Stuff Empid salary
1 Manager 1 5000
2 technical 2 3000
3 developer 3 2000



Add the resultset in some temporary table or use a derived table or a CTE & then use the pivot operator on it.

PBUH

Go to Top of Page
   

- Advertisement -