Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Sort data from Horizontal to Vertical ?

 
   Database Help (Home) -> Programming RSS
Next:  more ghosts, increasingly weird  
Author Message
Eng Teng

External


Since: May 08, 2008
Posts: 3



(Msg. 1) Posted: Tue Dec 02, 2008 9:26 pm
Post subject: Sort data from Horizontal to Vertical ?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

How can I sort the following data from Horizontal to Vertical using MS SQL ?

Table : Sample
From Horizontal
Type Value
A 1
B 2
C 3
D 4

To Vertical
Type A B C D
Value 1 2 3 4


Regard,
Tee

 >> Stay informed about: Sort data from Horizontal to Vertical ? 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 645



(Msg. 2) Posted: Tue Dec 02, 2008 9:35 pm
Post subject: Re: Sort data from Horizontal to Vertical ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Here is example with different methods for pivoting:

CREATE TABLE Foo (
foo_type CHAR(1) PRIMARY KEY,
foo_value INT);

INSERT INTO Foo VALUES('A', 1);
INSERT INTO Foo VALUES('B', 2);
INSERT INTO Foo VALUES('C', 3);
INSERT INTO Foo VALUES('D', 4);

-- pivot using CASE
SELECT MAX(CASE WHEN foo_type = 'A' THEN foo_value END) AS A,
MAX(CASE WHEN foo_type = 'B' THEN foo_value END) AS B,
MAX(CASE WHEN foo_type = 'C' THEN foo_value END) AS C,
MAX(CASE WHEN foo_type = 'D' THEN foo_value END) AS D
FROM Foo;

-- pivot using PIVOT operator (SQL Server 2005/2008)
SELECT A, B, C, D
FROM Foo
PIVOT
(MAX(foo_value) FOR foo_type IN (A, B, C, D)) AS P;

-- dynamic pivot (SQL Server 2005/2008)
DECLARE @pivot_cols NVARCHAR(1000);
SELECT @pivot_cols =
STUFF((SELECT DISTINCT '],[' + foo_type
FROM Foo
ORDER BY '],[' + foo_type
FOR XML PATH('')
), 1, 2, '') + ']';

DECLARE @pivot_query NVARCHAR(2000);
SET @pivot_query =
N'SELECT ' + @pivot_cols +
'FROM Foo ' +
'PIVOT ' +
'(MAX(foo_value) FOR foo_type IN (' + @pivot_cols + ')) AS P;';

EXEC(@pivot_query);

DROP TABLE Foo;

--
Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: Sort data from Horizontal to Vertical ? 
Back to top
Login to vote
steve dassin

External


Since: Feb 04, 2008
Posts: 42



(Msg. 3) Posted: Wed Dec 03, 2008 3:58 pm
Post subject: Re: Sort data from Horizontal to Vertical ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If you don't want to look at code, if some of it creeps you out, if you just
want to solve problems like this as quick and painlessly as possible..then
check out the RAC utility. Less coding=more productivity. It's the MS way:)

Visit RAC @
www.rac4sql.net

www.beyondsql.blogspot.com
 >> Stay informed about: Sort data from Horizontal to Vertical ? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Comparing horizontal and vertical data - I am working with data from two tables. The first table is the individual weights of product at each station in a food processing process. There may be 3 to 4000 records in a shift and are comprised of 24 stations. The second table is the sums of al...

How to sort this query - I have a simple query which returns me id (an integer), location_code (an integer), department_code (an integer), date_worked (a datatime) and hours_worked (a decimal): select ts.id, ts.location_code, ts.department_code, ts.date_worked, ts.hours_worke...

Help with distinct + sort order - Hi I've the following set of un-normalised records. SortID Customer 1 ccc 2 ccc 3 aaa 4 aaa 5 bbb 6 bbb I need to perform a select which will return distinct Customer name while ..

Sort order and view - HI all, Using SQL 2005, I create a simple view Select * from employee order by name But when I select the view with select * from employeeview the sort order is not adheered to. I have to specificcally issue a order by on the view Can someone explain...

A Unique Constraint (sort of) - Hi all, i'm trying to prevent duplicate entries into a column yet still allow many NULL values in that column. (table and data script below) this didn't work (because of duplicate NULLS) ALTER TABLE TestTab ADD CONSTRAINT IX_TestTab_Col1 UNIQUE..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]