DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to SELECT with UNPIVOT from a table?

Execute the following SQL Server T-SQL scripts in Management Studio Query Editor to demonstrate the UNPIVOT operator applications.

USE AdventureWorks2008;

/*  TABLE DEFINTION

CREATE TABLE Production.ProductPhoto(

      ProductPhotoID int IDENTITY(1,1) PRIMARY KEY,

      ThumbNailPhoto varbinary(max),

      ThumbnailPhotoFileName nvarchar(50),

      LargePhoto varbinary(max),

      LargePhotoFileName nvarchar(50),

      ModifiedDate datetime default (getdate()));

*/

-- T-SQL UNPIVOT operator to transpose columns into rows

SELECT ProductPhotoID, VALUE

FROM Production.ProductPhoto

UNPIVOT

(

 value FOR attribute IN([ThumbnailPhotoFileName], [LargePhotoFileName])

) AS upv

ORDER BY ProductPhotoID, VALUE;

/* ProductPhotoID       VALUE

      1           no_image_available_large.gif

      1           no_image_available_small.gif

      69          racer02_black_f_large.gif

      69          racer02_black_f_small.gif

      70          racer02_black_large.gif

      70          racer02_black_small.gif

.... */

Related link: http://www.sqlusa.com/bestpractices/training/scripts/pivotunpivot/

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE