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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:14:57+00:00 2026-06-01T11:14:57+00:00

Another stupid question here regarding a SQL scenario. Given data such as: FieldKey DocumentKey

  • 0

Another stupid question here regarding a SQL scenario.

Given data such as:

FieldKey    DocumentKey FieldId FieldValue
1   00001c55-aab3-4df8-a07e-8eac162fa075    TITLE   Mass Import 18355
2   00001c55-aab3-4df8-a07e-8eac162fa075    1   00001c55-aab3-4df8-a07e-8eac162fa075
3   00001c55-aab3-4df8-a07e-8eac162fa075    2   9F-2F-CF-76-27-E7-5B-C9-27-CE-23-45-68-3F-E2-89
4   00001c55-aab3-4df8-a07e-8eac162fa075    3   18355
5   00001c55-aab3-4df8-a07e-8eac162fa075    4   94-3C-84-B1-6A-AA-FD-25-F1-C0-D2-43-CD-D3-57-D6
6   00001c55-aab3-4df8-a07e-8eac162fa075    5   Created by C# mass import
7   00002205-00D3-4495-B65A-A7B1FD2AE7F2    TITLE   Mass Import 1494780
8   00002205-00D3-4495-B65A-A7B1FD2AE7F2    1   00002205-00D3-4495-B65A-A7B1FD2AE7F2
9   00002205-00D3-4495-B65A-A7B1FD2AE7F2    2   870386312
10  00002205-00D3-4495-B65A-A7B1FD2AE7F2    3   1494780
11  00002205-00D3-4495-B65A-A7B1FD2AE7F2    4   -1929051324
13  00002342-6de0-4110-b576-fd32f96b2858    TITLE   Mass Import 387008
14  00002342-6de0-4110-b576-fd32f96b2858    1   00002342-6de0-4110-b576-fd32f96b2858
15  00002342-6de0-4110-b576-fd32f96b2858    2   B0-CB-DF-ED-48-DC-C4-E8-B0-6F-1B-1D-81-2D-6D-51
16  00002342-6de0-4110-b576-fd32f96b2858    3   387008

With table structure:

[FieldKey] [bigint] IDENTITY(1,1) NOT NULL,
[DocumentKey] [char](36) NOT NULL,
[FieldId] [varchar](10) NOT NULL,
[FieldValue] [varchar](255) NOT NULL

What is the best way to flip this data horizontally? Ideally:

DocumentKey                             TITLE    1    2    3    4    5
00001c55-aab3-4df8-a07e-8eac162fa075    mytitle  val1 val2 val3 val4 val5
00002205-00D3-4495-B65A-A7B1FD2AE7F2    mytitle2 val6 val7 val8 val9
00002342-6de0-4110-b576-fd32f96b2858    mytitle3 vl10 vl11 vl12

I thought a pivot table might work and it is close, but the problem is I don’t need an aggregation which is required for a SQL pivot. I just want to flip the data. The number of columns (1-5 plus TITLE in my example) could be anywhere between TITLE plus 1-30.

Maybe I’m just being dense, but any ideas would be appreciated.

  • 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-06-01T11:14:58+00:00Added an answer on June 1, 2026 at 11:14 am

    I think that you are on the right track with a PIVOT. You are going to have a aggregate on the value column. But as you say you will have 1-30 column plus the title column. That requires a dynamic pivot

    Here is a suggestion (that is only for SQL Server 2005+):

    Test data

    CREATE TABLE Table1
    (
        [FieldKey] [bigint] NOT NULL,
        [DocumentKey] [char](36) NOT NULL,
        [FieldId] [varchar](10) NOT NULL,
        [FieldValue] [varchar](255) NOT NULL
    )
    INSERT INTO Table1
    VALUES
        (1,'00001c55-aab3-4df8-a07e-8eac162fa075','TITLE','Mass Import 18355'),
        (2,'00001c55-aab3-4df8-a07e-8eac162fa075','1','00001c55-aab3-4df8-a07e-8eac162fa075'),
        (3,'00001c55-aab3-4df8-a07e-8eac162fa075','2','9F-2F-CF-76-27-E7-5B-C9-27-CE-23-45-68-3F-E2-89'),
        (4,'00001c55-aab3-4df8-a07e-8eac162fa075','3','18355'),
        (5,'00001c55-aab3-4df8-a07e-8eac162fa075','4','94-3C-84-B1-6A-AA-FD-25-F1-C0-D2-43-CD-D3-57-D6'),
        (6,'00001c55-aab3-4df8-a07e-8eac162fa075','5','Created by C# mass import'),
        (7,'00002205-00D3-4495-B65A-A7B1FD2AE7F2','TITLE','Mass Import 1494780'),
        (8,'00002205-00D3-4495-B65A-A7B1FD2AE7F2','1','00002205-00D3-4495-B65A-A7B1FD2AE7F2'),
        (9,'00002205-00D3-4495-B65A-A7B1FD2AE7F2','2','870386312'),
        (10,'00002205-00D3-4495-B65A-A7B1FD2AE7F2','3','1494780'),
        (11,'00002205-00D3-4495-B65A-A7B1FD2AE7F2','4','-1929051324'),
        (13,'00002342-6de0-4110-b576-fd32f96b2858','TITLE','Mass Import 387008'),
        (14,'00002342-6de0-4110-b576-fd32f96b2858','1','00002342-6de0-4110-b576-fd32f96b2858'),
        (15,'00002342-6de0-4110-b576-fd32f96b2858','2','B0-CB-DF-ED-48-DC-C4-E8-B0-6F-1B-1D-81-2D-6D-51'),
        (16,'00002342-6de0-4110-b576-fd32f96b2858','3','387008')
    

    Find the unique columns

    DECLARE @cols VARCHAR(MAX)
    ;WITH CTE
    AS
    (
        SELECT
            ROW_NUMBER() OVER(PARTITION BY Table1.FieldId 
                                  ORDER BY Table1.FieldId) AS RowNbr,
            Table1.*
        FROM
            Table1
    )
    SELECT @cols=STUFF
    (
        (
            SELECT 
                ',' +QUOTENAME(CTE.FieldId)
            FROM
                CTE
            WHERE
                CTE.RowNbr=1
            ORDER BY 
                LEN(CTE.FieldId) DESC,
                CTE.FieldId
            FOR XML PATH('')
        )
    ,1,1,'')
    

    We need to order so that TITLE comes first. The FOR XML PATH is to concat the columns.

    Dynamic pivot

    DECLARE @query NVARCHAR(4000)=
    N'SELECT
        *
    FROM
    (
        SELECT
            Table1.DocumentKey,
            Table1.FieldId,
            Table1.FieldValue
        FROM
            Table1
    ) AS p
    PIVOT
    (
        MAX(FieldValue)
        FOR FieldId IN('+@cols+')
    ) AS p'
    EXECUTE(@query)
    

    Clean up after myself (not required)

    DROP TABLE Table1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Masters Of Web Programming, here I come with another stupid question, hoping someone
Hello another stupid question regarding leaks and also NSURLConnection. How do i release it?
Here's another really stupid question. Xcode has a Developer Documentation window. There are sections
Probably a stupid question, but is there a Javascript method to browse another url,
sorry if you'll find this question stupid, but I really need help. Here's some
It might be a stupid question, but here goes: Is it possible to make
This is an offshoot question that's related to another I asked here . I'm
We've got two developers on the same closed (ugh, stupid gov) network, Another developer
Another poster asked about preferred syntax for infinite loops . A follow-up question: Why
Another question asked about determining odd/evenness in C, and the idiomatic (x & 1)

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.