How to make pivot.


SELECT   propertyid AS PropertyID,
[2] as 'Type2',[7]as 'Type7',[8] as 'Type8'
FROM
(SELECT prop.propertyid,prop.relationshiptypeid
    FROM  vw_SpaceContactRelationship as prop
   
    ) AS SourceTable
PIVOT
(
MAX(relationshiptypeid)
FOR relationshiptypeid  IN ([2],[7],[8])
) AS PivotTable


RESULT OF THIS QUERY

PropertyID          Type2    Type7    Type8
707312                  NULL     7             8
707313                  NULL     7         NULL
707329                  2             7         NULL
707344                  NULL     NULL     8
707345                  NULL     7             8
707361                  NULL     7        NULL
707444                  2             7        NULL




Comments

  1. SELECT *
    FROM ( SELECT DISTINCT
    p.* ,
    r.RegionName ,
    CASE WHEN r.RegionID = pr.RegionID THEN 1
    ELSE 0
    END AS assignedBit
    FROM dbo.Product p
    LEFT OUTER JOIN ProductRegions pr ( NOLOCK ) ON pr.ProductID = p.ProductID
    CROSS JOIN Regions r ( NOLOCK )
    ) AS s PIVOT
    ( MAX(assignedBit) FOR [RegionName] IN ( "North Asia", "North Europe" ) )AS pivots



    ReplyDelete

Post a Comment

Popular posts from this blog

Grouping Sets, With Rollup and With Cube example

Adding Page brake aganist group values

To Get All the Names of tables and Databases their data have changed in last 24 Hours And Backup those databases