SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to apply PIVOT for many-to-many relationship?

Execute the following SQL Server T-SQL scripts in Microsoft SSMS Query Editor to demonstrate the application of the PIVOT operator for the generation of a crosstab query with Boolean result values.

-- SQL crosstab with pivot for Boolean values

-- SQL select into create table

USE tempdb;

SELECT DISTINCT Category = psc.Name,

                Size

INTO   SubCategorySizeUsage

FROM   AdventureWorks2008.Production.Product p

    INNER JOIN AdventureWorks2008.Production.ProductSubcategory psc

        ON p.ProductSubcategoryID = psc.ProductSubcategoryID

WHERE  Size IS NOT NULL

       AND p.ProductSubcategoryID IS NOT NULL

GO

-- T-SQL select to check sample data in rows (vertical)  

SELECT   *

FROM     SubCategorySizeUsage

ORDER BY Category

GO

 

/*  

Category    Size

Bib-Shorts  L

Bib-Shorts  M

Bib-Shorts  S

Gloves      L

Gloves      M

Gloves      S

.....

*/

 

-- T-SQL hard-wired column list for the PIVOT operator

DECLARE  @Sizes VARCHAR(MAX)

SET @Sizes = ''

SELECT @Sizes = @Sizes + ', ' + QUOTENAME(Size)

FROM   (SELECT DISTINCT Size

        FROM   SubCategorySizeUsage) x

SELECT @Sizes = STUFF(@Sizes,1,1,'')

SELECT @Sizes

GO

/* PIVOT list

 [38], [40], [42], [44], [46], [48], [50], [52], [54], [56], [58], [60], [62], [70], [L], [M], [S], [XL]

  */

SELECT pvt.*

FROM   (SELECT Category,

               Size,

               SizeExist = 1

        FROM   SubCategorySizeUsage

        UNION

        SELECT x.Category,

               y.Size,

               0

        FROM   SubCategorySizeUsage x

               CROSS JOIN SubCategorySizeUsage y) cc

       PIVOT

       (sum(SizeExist)

        FOR Size IN (  [38], [40], [42], [44], [46], [48], [50], [52], [54], [56], [58], [60], [62], [70], [L], [M], [S], [XL]) ) AS pvt

GO

 

/* Results: Many-to-many relationship in crosstab(matrix) report format

            0 means no relationship, 1 means relationship

 

 

Category 38 40 42 44 46 48 50 52 54 56 58 60 62 70 L M S XL
Bib-Shorts 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
Gloves 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
Hydration Packs 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
Jerseys 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
Mountain Bikes 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0
Mountain Frames 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0
Road Bikes 1 1 1 1 0 1 0 1 0 1 1 1 1 0 0 0 0 0
Road Frames 1 1 1 1 0 1 0 1 0 1 1 1 1 0 0 0 0 0
Shorts 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
Socks 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
Tights 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
Touring Bikes 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0
Touring Frames 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0
Vests 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0

*/

 

-- Cleanup

DROP TABLE SubCategorySizeUsage

GO

 

Related link: http://www.sqlusa.com/bestpractices2005/crosstabwithpivot/

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.