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 7514221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:25:58+00:00 2026-05-30T00:25:58+00:00

— Pivot table with one row and four columns SELECT ‘Values’ tValues, ID,Name,ValueID,Value FROM

  • 0
 -- Pivot table with one row and four columns

SELECT 'Values' tValues, 
    ID,Name,ValueID,Value FROM (

        Select ID,Name,ValueID,Value FROM Table WHERE OptionID = 1000000

    ) AS SourceTable 

    PIVOT ( 

        COUNT(tValues)
        FOR tValues IN ( ID,Attribute,ValueID,Value ) 

    ) AS PivotTable;

I’m going off the example at Microsoft.com: http://msdn.microsoft.com/en-us/library/ms177410.aspx

But there are a few things about Pivot i don’t really understand, so don’t be surprised when you see it in the code above, such as COUNT(tValues), I have no idea what this is for, by judging from the example on microsoft, it seems to be always some sort of numeric value, so i figured i’d try it to see if it would return something, but all it returns is an error. Anyhow, if someone out there can share why this query doesn’t work, and possibly explain what the numeric value above the FOR is used for?

The Table containts an x amount of rows, with four columns, so it looks like this:

 ID  |  Name   |  ValueID  |  Value

 100 |  Color  |  10000    |  Black
 101 |  Size   |  10005    |  Large

The output should be like this:

 Name_100  |  Color   |  Name_101  |  Size  |

 10000     |  Black   |  10005     |  Large |
  • 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-30T00:25:59+00:00Added an answer on May 30, 2026 at 12:25 am

    Something like this maybe.

    This will only work if the name column is unique. If not then you might want to append an id on it.

    So first some test data:

    CREATE TABLE tblValues
        (
            ID INT,
            Name VARCHAR(100),
            ValueID INT, 
            Value VARCHAR(100)
        )
    
    INSERT INTO tblValues
    VALUES
        (100,'Color',10000,'Black'),
        (101,'Size',10005,'Large')
    

    Then you need to get the columns to pivot on:

    DECLARE @cols VARCHAR(MAX)
    ;WITH CTE AS
    (
        SELECT
            'Name_'+CAST(tbl.ID AS VARCHAR(100)) AS Name,
            'Name_'+CAST(tbl.ID AS VARCHAR(100)) AS Sort,
            tbl.ID
        FROM
            tblValues AS tbl
        UNION ALL
        SELECT
            tbl.Name,
            'Value_'+CAST(tbl.ID AS VARCHAR(100)) AS Sort,
            tbl.ID
        FROM
            tblValues AS tbl
    )
    SELECT
        @cols = COALESCE(@cols + ','+QUOTENAME(Name),
                     QUOTENAME(Name))
    FROM
        CTE
    ORDER BY
        CTE.ID,
        CTE.Sort
    

    Then declaring and executing the dynamic sql like this:

    DECLARE @query NVARCHAR(4000)=
    N'SELECT
        *
    FROM
    (
        SELECT
            ''Name_''+CAST(tbl.ID AS VARCHAR(100)) AS pivotName,
            CAST(tbl.ValueID AS VARCHAR(100)) AS name
        FROM
            tblValues AS tbl
        UNION ALL
        SELECT
            tbl.Name AS pivotName,
            tbl.Value AS name
        FROM
            tblValues AS tbl
    ) AS p
    PIVOT
    (
        MAX(name)
        FOR pivotName IN ('+@cols+')
    ) AS pvt'
    
    EXECUTE(@query)
    

    Then in my case I will drop the table I have created

    DROP TABLE tblValues
    

    Edit

    Or in you case it should be something like this:

    First the columns:

    DECLARE @cols VARCHAR(MAX)
    ;WITH CTE AS
    (
        SELECT
            'Name_'+CAST(tbl.ID AS VARCHAR(100)) AS Name,
            'Name_'+CAST(tbl.ID AS VARCHAR(100)) AS Sort,
            tbl.ID
        FROM
            [Table] AS tbl
        WHERE
            tbl.OptionID = 1000000
        UNION ALL
        SELECT
            tbl.Name,
            'Value_'+CAST(tbl.ID AS VARCHAR(100)) AS Sort,
            tbl.ID
        FROM
            [Table] AS tbl
        WHERE
            tbl.OptionID = 1000000
    )
    SELECT
        @cols = COALESCE(@cols + ','+QUOTENAME(Name),
                     QUOTENAME(Name))
    FROM
        CTE
    ORDER BY
        CTE.ID,
        CTE.Sort
    

    Then the dynamic sql.

    DECLARE @query NVARCHAR(4000)=
    N'SELECT
        *
    FROM
    (
        SELECT
            ''Name_''+CAST(tbl.ID AS VARCHAR(100)) AS pivotName,
            CAST(tbl.ValueID AS VARCHAR(100)) AS name
        FROM
            [Table] AS tbl
        WHERE
            tbl.OptionID = 1000000
        UNION ALL
        SELECT
            tbl.Name AS pivotName,
            tbl.Value AS name
        FROM
            [Table] AS tbl
        WHERE
            tbl.OptionID = 1000000
    ) AS p
    PIVOT
    (
        MAX(name)
        FOR pivotName IN ('+@cols+')
    ) AS pvt'
    
    EXECUTE(@query)
    

    You do not need to create the table or drop the table. That was just because I did not have your table in my database and that if someone else want’s to run the example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

— or — Is there a difference between these? Is one better-supported than the
I'm using some of Firefox's specially-defined values for cursor, in particular -moz-zoom-in -moz-zoom-out -moz-grab
By fixed vector I mean a static list of values, like 1 through 24.
I'm using a DatePicker ( org.apache.wicket.extensions.yui.calendar.DatePicker — Javadoc ) in a form. The form
string formIdList = 8256, 8258, 8362, 8120, 8270, 8271, 8272, 8273, 8257, 8279, 8212,
Basically, is there a way to write a.children('.outer').children('.inner') without the intermediate selector? I can't
So to start, I have an array of XML files. These files need to
I recently ran against a very interesting site that expresses a very interesting idea
I'm creating a FORM with PHP for an intranet webpage. Another PHP script is
All of my code is on my SVN, but I want some of it

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.