SQL Server 2012 Pivot Table

RustyHamster

I need to create a query that shows the results in a pivot table.

I have a table that is updated regularly with clients Build Status Changes

WE have 8 stages to a build

  • 115 Land Purchased
  • 116 Foundations
  • 117 Timber Kit Erected / Wall Plate Level
  • 118 Wind & Water Tight
  • 119 1st Fix & Plastering
  • 120 Final Fit Out
  • 121 Completed
  • 122 Redeemed

Here is my query

SELECT 
    s.ProjectStage, su.DateStageChanged, p.FK_ID As ID,
    s.LongDesc AS BuildType, su.FK_StageID  
FROM 
    [dbo].[tbl_StageUpdates] AS su
LEFT JOIN
    [dbo].[tbl_Stage] AS s ON su.FK_StageID = s.id
LEFT JOIN 
    tbl_Projects AS p ON su.FK_ProjectID = p.PK_ProjectID
WHERE 
    s.LongDesc = 'New Build'
GROUP BY 
    p.FK_ID, s.ProjectStage, su.DateStageChanged, s.LongDesc, su.FK_StageID
ORDER BY 
    su.FK_StageID ASC

ID is the clients ID what i would like is have the query display the information like this:

enter image description here

Is it possible to do?

Any help on this would be greatly appreciated

Cheers

Trung Duong

If you've only 8 stages, you could use CASE...WHEN and GROUP BY to archive your expected output. I think it's more readable than using PIVOT function. Furthermore, it's T-SQL and be easier to migrate to other DBMS.

SELECT 
   ID, 
   BuildType,
   MAX(CASE FK_StageID WHEN 115 THEN DateStageChanged ELSE NULL END) AS [Land Purchased],
   MAX(CASE FK_StageID WHEN 116 THEN DateStageChanged ELSE NULL END) AS [Foundations],
   MAX(CASE FK_StageID WHEN 117 THEN DateStageChanged ELSE NULL END) AS [Timber Kit Erected / Wall Plate Level],
   MAX(CASE FK_StageID WHEN 118 THEN DateStageChanged ELSE NULL END) AS [Wind & Water Tight],
   MAX(CASE FK_StageID WHEN 119 THEN DateStageChanged ELSE NULL END) AS [1st Fix & Plastering],
   MAX(CASE FK_StageID WHEN 120 THEN DateStageChanged ELSE NULL END) AS [Final Fit Out],
   MAX(CASE FK_StageID WHEN 121 THEN DateStageChanged ELSE NULL END) AS [Completed],
   MAX(CASE FK_StageID WHEN 122 THEN DateStageChanged ELSE NULL END) AS [Redeemed]
FROM
   (
    -- original query  
        SELECT 
            s.ProjectStage, su.DateStageChanged, p.FK_ID As ID,
            s.LongDesc AS BuildType, su.FK_StageID  
        FROM 
            [dbo].[tbl_StageUpdates] AS su
        LEFT JOIN
            [dbo].[tbl_Stage] AS s ON su.FK_StageID = s.id
        LEFT JOIN 
            tbl_Projects AS p ON su.FK_ProjectID = p.PK_ProjectID
        WHERE 
            s.LongDesc = 'New Build'
        GROUP BY 
            p.FK_ID, s.ProjectStage, su.DateStageChanged, s.LongDesc, su.FK_StageID
    -- original query           
   )  Data 
GROUP BY 
   ID, BuildType

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL Server 2012 Pivot Dynamic with Values?

分類Dev

Pivot table in SQL Server 2008

分類Dev

SQL server Pivot Table issue

分類Dev

SQL Server2012の日付列をPIVOT

分類Dev

SQL SERVER PIVOT TABLE1行

分類Dev

SQL Server Two Column Pivot table

分類Dev

SQL server Pivot Table columns from other table

分類Dev

How to get COUNT(*) from one partition of a table in SQL Server 2012?

分類Dev

Delete duplicate records from SQL Server 2012 table with identity

分類Dev

Sql server pivot solution

分類Dev

pivot or un pivot sql server2005

分類Dev

Pivot SQL table to N columns

分類Dev

SQL Server pivot text values

分類Dev

Trouble with SQL Server Dynamic Pivot

分類Dev

Numbering islands in SQL Server 2012

分類Dev

Restore SQL Server 2012 error

分類Dev

SQL Server 2012: How to get up to 5 hierarchy levels of data from original to derived product table

分類Dev

Why joining two table variables significantly increases execution time in SQL Server 2012

分類Dev

Pivot in SQL Server T-SQL

分類Dev

LINQ to SQL Pivot table in VB.net

分類Dev

SQL Server : Crosstab/Pivot Price List

分類Dev

SQL Server 2008 Pivot, no Aggregation, complex data

分類Dev

SQL Server Dynamic Pivot Column Names

分類Dev

sql server - transforming data with dynamic pivot

分類Dev

No process is on the other end of the pipe (SQL Server 2012)

分類Dev

SQL Server2012のRANDBETWEEN

分類Dev

Requires SQL Server 2012 Express LocalDB

分類Dev

SQL Server 2012のJSON_VALUE?

分類Dev

How to check history in SQL Server 2012?