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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:10:47+00:00 2026-06-18T08:10:47+00:00

I’ve got two tables: TableA and TableB TableA: columnBLA, objName, objID TableB: objID, ColumnIndex,

  • 0

I’ve got two tables: TableA and TableB

TableA: columnBLA, objName, objID

TableB: objID, ColumnIndex, StringValue, NumberValue, DateValue

TableA has a row: bla, Object Name, 21

TableB also has data for objID=21:

21, 1, a, NULL, NULL
21, 1, b, NULL, NULL
21, 1, c, NULL, NULL
21, 2, NULL, 11, NULL
21, 2, NULL, 22, NULL
21, 2, NULL, 33, NULL
21, 3, NULL, NULL, 1/1/2012
21, 3, NULL, NULL, 1/1/2013
21, 3, NULL, NULL, 1/1/2014

Now I want to reshape this data to the following form:

a, 11, 1/1/2012
b, 22, 1/1/2013
c, 33, 1/1/2014

I’ve got this far:

Select StringValue, NumberValue, DateValue
From
(Select StringValue
From TableA ta WITH (NOLOCK), TableB tb WITH (NOLOCK)
WHERE ta.objID = tb.objID
AND t.objName = N'Object Name'
AND d.ColumnIndex = 1) As StringValues
,
(Select NumberValue
From TableA ta WITH (NOLOCK), TableB tb WITH (NOLOCK)
WHERE ta.objID = tb.objID
AND t.objName = N'Object Name'
AND d.ColumnIndex = 2) As NumberValues
,
(Select DateValue
From TableA ta WITH (NOLOCK), TableB tb WITH (NOLOCK)
WHERE ta.objID = tb.objID
AND t.objName = N'Object Name'
AND d.ColumnIndex = 1) As DateValues

But I got unwanted result.
Somebody told me I should use a PIVOT for that, but My SQL knowledge doesn’t span that far.

  • 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-18T08:10:48+00:00Added an answer on June 18, 2026 at 8:10 am

    Here is one way to do this using both an UNPIVOT and a PIVOT:

    select objid, stringvalue, numbervalue, datevalue
    from
    (
      select objid, col, value,
        row_number() over(partition by objid, col order by value) rn
      from
      (
        select a.objid,
          b.stringvalue,
          b.numbervalue,
          b.datevalue
        from tablea a
        left join tableb b
          on a.objid = b.objid
      ) src
      unpivot
      (
        value
        for col in (stringvalue, numbervalue, datevalue)
      ) unpiv
    ) s
    pivot
    (
      max(value)
      for col in (stringvalue, numbervalue, datevalue)
    ) piv
    

    See SQL Fiddle with Demo. The unpivot takes the values from your separate columns and converts it into rows. I then apply a row_number() to the data and then apply the pivot to turn it back into columns.

    Another way to do this without using the PIVOT and UNPIVOT functions is to apply a row_number() and then use an aggregate function with a CASE expression:

    select objid,
      max(case when columnindex = 1 then stringvalue end) stringvalue,
      max(case when columnindex = 2 then numbervalue end) numbervalue,
      max(case when columnindex = 3 then datevalue end) datevalue
    from
    (
      select a.objid,
        b.stringvalue,
        b.numbervalue,
        b.datevalue,
        b.columnindex,
        row_number() over(partition by a.objid, b.columnindex 
                          order by b.columnindex) rn
      from tablea a
      left join tableb b
        on a.objid = b.objid
    ) src
    group by objid, rn
    

    See SQL Fiddle with Demo

    The result of both query is:

    | OBJID | STRINGVALUE | NUMBERVALUE | DATEVALUE |
    -------------------------------------------------
    |    21 |           a |          11 |  1/1/2012 |
    |    21 |           b |          22 |  1/1/2013 |
    |    21 |           c |          33 |  1/1/2014 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
In my XML file chapters tag has more chapter tag.i need to display chapters
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was
i got an object with contents of html markup in it, for example: string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.