Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 810247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:51:11+00:00 2026-05-15T00:51:11+00:00

Is it actually possible to rotate a T-SQL (2005) so that (for the sake

  • 0

Is it actually possible to rotate a T-SQL (2005) so that (for the sake of argument) the values of the first column’s rows become the titles of the output table’s columns?

I realise this is not really what PIVOT is for, but it’s what I need – the ability to request a table where the columns are not known before-hand because they have been entered as values into a table.

Even a hack would be nice, tbh.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:51:12+00:00Added an answer on May 15, 2026 at 12:51 am

    Itzik Ben-Gan’s example on how to build dynamic PIVOT, I highly recommend his Inside Microsoft SQL Server 2008: T-SQL Programming book

    -- Creating and Populating the Orders Table
    USE tempdb;
    GO
    
    IF OBJECT_ID('dbo.Orders') IS NOT NULL
    DROP TABLE dbo.Orders;
    GO
    
    CREATE TABLE dbo.Orders
    (
    orderid   int        NOT NULL PRIMARY KEY NONCLUSTERED,
    orderdate datetime   NOT NULL,
    empid     int        NOT NULL,
    custid    varchar(5) NOT NULL,
    qty       int        NOT NULL
    );
    
    CREATE UNIQUE CLUSTERED INDEX idx_orderdate_orderid
    ON dbo.Orders(orderdate, orderid);
    
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(30001, '20020802', 3, 'A', 10);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(10001, '20021224', 1, 'A', 12);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(10005, '20021224', 1, 'B', 20);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(40001, '20030109', 4, 'A', 40);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(10006, '20030118', 1, 'C', 14);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(20001, '20030212', 2, 'B', 12);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(40005, '20040212', 4, 'A', 10);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(20002, '20040216', 2, 'C', 20);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(30003, '20040418', 3, 'B', 15);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(30004, '20020418', 3, 'C', 22);
    INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
    VALUES(30007, '20020907', 3, 'D', 30);
    GO
    
    -- Static PIVOT
    SELECT *
    FROM (SELECT custid, YEAR(orderdate) AS orderyear, qty
    FROM dbo.Orders) AS D
    PIVOT(SUM(qty) FOR orderyear IN([2002],[2003],[2004])) AS P;
    GO
    
    -- Dynamic PIVOT
    DECLARE @T AS TABLE(y INT NOT NULL PRIMARY KEY);
    
    DECLARE
    @cols AS NVARCHAR(MAX),
    @y    AS INT,
    @sql  AS NVARCHAR(MAX);
    
    -- Construct the column list for the IN clause
    -- e.g., [2002],[2003],[2004]
    SET @cols = STUFF(
    (SELECT N',' + QUOTENAME(y) AS [text()]
    FROM (SELECT DISTINCT YEAR(orderdate) AS y FROM dbo.Orders) AS Y
    ORDER BY y
    FOR XML PATH('')),
    1, 1, N'');
    
    -- Construct the full T-SQL statement
    -- and execute dynamically
    SET @sql = N'SELECT *
    FROM (SELECT custid, YEAR(orderdate) AS orderyear, qty
    FROM dbo.Orders) AS D
    PIVOT(SUM(qty) FOR orderyear IN(' + @cols + N')) AS P;';
    
    EXEC sp_executesql @sql;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all I am not sure whether it is actually possible in javascript,
Is it possible in Android to capture screen rotate events but not actually have
First, I don't know if this is actually possible but what I want to
I'm wondering if it's actually possible to draw a polygon over a complete town
I would like to know whether this is actually possible in Android? When Android
Is it possible to actually make use of placement new in portable code when
Is it possible to actually use the type passed as a template for control
Would it be possible through c# to actually do a windows search (the one
2 Questions actually: I know i must use Stored Procedures as much as Possible,
I had a quick question. Is it possible to save a file without actually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.