Additional column in unpivot/pivot in MSSQLPassing column names dynamically to UNPIVOTPivot column...

What does Cypher mean when he says Neo is "gonna pop"?

Why avoid shared user accounts?

Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?

Quenching swords in dragon blood; why?

Parsing a string of key-value pairs as a dictionary

Notes in a lick that don't fit in the scale associated with the chord

Are there neural networks with very few nodes that decently solve non-trivial problems?

Can a person refuse a presidential pardon?

Can an insurance company drop you after receiving a bill and refusing to pay?

Using only 1s, make 29 with the minimum number of digits

What to do if authors don't respond to my serious concerns about their paper?

Is there some relative to Dutch word "kijken" in German?

Pre-1980's science fiction short story: alien disguised as a woman shot by a gangster, has tentacles coming out of her breasts when remaking her body

Why Normality assumption in linear regression

Difference between two quite-similar Terminal commands

What is the most triangles you can make from a capital "H" and 3 straight lines?

Program that converts a number to a letter of the alphabet

Why did other German political parties disband so fast when Hitler was appointed chancellor?

Slow moving projectiles from a hand-held weapon - how do they reach the target?

How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?

What is the wife of a henpecked husband called?

If I sold a PS4 game I owned the disc for, can I reinstall it digitally?

We are very unlucky in my court

Can a hotel cancel a confirmed reservation?



Additional column in unpivot/pivot in MSSQL


Passing column names dynamically to UNPIVOTPivot column querySimple Unpivot - erroneous Column ConflictHow to pivot this unpivot?How to PIVOT/UNPIVOT if I don't know how many columns will it have?Linear interpolation without using pivot and unpivotDBCC ShrinkFile not workingSQL query to convert Column to Row using Pivot/Unpivotunpivot 2 columns into one column













0















;with #unpivot_step2 as
(
select reqid, Idno, RowNum, colName, Vals
From
(
select
cast(reqid as varchar(150)) AS reqid,
cast(name collate database_default as varchar (150)) as name,
cast(idno collate database_default as varchar (150)) as idno,
cast(basicgroup collate database_default as varchar (150)) as basicgroup,
cast(industrialsector collate database_default as varchar (150)) as industrialsector,
cast(gender collate database_default as varchar (150)) as gender,
cast(income collate database_default as varchar (150)) as income,
cast(empname collate database_default as varchar(150)) as empname,
cast(emptyp collate database_default as varchar (150)) as emptyp,
row_number() over (partition by idno order by cast(date_created as datetime) desc) as RowNum
from #mydatabase
) unpivot_table
unpivot
(
vals for colName in (name, basicgroup, industrialsector, gender, income, empname, emptyp)
) unpivot_handle
)

select reqid, idno, ColName, [2] as [From], [1] as [To]
FROM
(
select reqid, idno, rownum, colname, vals
from #unpivot_step2
) pivot_table
pivot
(
max (vals) for RowNum in ([1],[2])
) pivot_handle
where [1] <> [2]


With this script,I got result as below:



+-----------+--------------+------------------+----------------------+----------------------+
| reqid | idno | colName | From_Value | To_Value |
+-----------+--------------+------------------+----------------------+----------------------+
| 170916258 | 050505050505 | empname | | YOLO |
| 170916258 | 050505050505 | emptyp | | 113 |
| 170916258 | 050505050505 | gender | P | |
| 170916258 | 050505050505 | income | 8891126 | |
| 170916258 | 050505050505 | name | TEST2 | TEST3_2 |
| 170916258 | 100202025698 | gender | L | P |
| 170916258 | 100202025698 | industrialsector | 01261 | |
| 170916258 | 100202025698 | name | APPLICANT5_2 | CUST3_6 |
| 170916258 | 260404045698 | gender | | P |
| 170916258 | 260404045698 | industrialsector | | 01279 |
| 170916258 | 260404045698 | name | CUST4_3 | EHH4_4 |
+-----------+--------------+------------------+----------------------+----------------------+


How can I get result like below:



+-----------+---------+--------------+------------------+--------------+----------+
| reqid | Name | idno | colName | From_Value | To_Value |
+-----------+---------+--------------+------------------+--------------+----------+
| 170916258 | TEST3_2 | 50505050505 | empname | | YOLO |
| 170916258 | TEST3_2 | 50505050505 | emptyp | | 113 |
| 170916258 | TEST3_2 | 50505050505 | gender | P | |
| 170916258 | TEST3_2 | 50505050505 | income | 8891126 | |
| 170916258 | TEST3_2 | 50505050505 | name | TEST2 | TEST3_2 |
| 170916258 | EHH4_4 | 100202025698 | gender | L | P |
| 170916258 | EHH4_4 | 100202025698 | industrialsector | 1261 | |
| 170916258 | EHH4_4 | 100202025698 | name | APPLICANT5_2 | CUST3_6 |
| 170916258 | EHH4_4 | 260404045698 | gender | | P |
| 170916258 | EHH4_4 | 260404045698 | industrialsector | | 1279 |
| 170916258 | EHH4_4 | 260404045698 | name | CUST4_3 | EHH4_4 |
+-----------+---------+--------------+------------------+--------------+----------+


I want additional column "NAME". Any idea how to enhance this script?









share









New contributor




user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    ;with #unpivot_step2 as
    (
    select reqid, Idno, RowNum, colName, Vals
    From
    (
    select
    cast(reqid as varchar(150)) AS reqid,
    cast(name collate database_default as varchar (150)) as name,
    cast(idno collate database_default as varchar (150)) as idno,
    cast(basicgroup collate database_default as varchar (150)) as basicgroup,
    cast(industrialsector collate database_default as varchar (150)) as industrialsector,
    cast(gender collate database_default as varchar (150)) as gender,
    cast(income collate database_default as varchar (150)) as income,
    cast(empname collate database_default as varchar(150)) as empname,
    cast(emptyp collate database_default as varchar (150)) as emptyp,
    row_number() over (partition by idno order by cast(date_created as datetime) desc) as RowNum
    from #mydatabase
    ) unpivot_table
    unpivot
    (
    vals for colName in (name, basicgroup, industrialsector, gender, income, empname, emptyp)
    ) unpivot_handle
    )

    select reqid, idno, ColName, [2] as [From], [1] as [To]
    FROM
    (
    select reqid, idno, rownum, colname, vals
    from #unpivot_step2
    ) pivot_table
    pivot
    (
    max (vals) for RowNum in ([1],[2])
    ) pivot_handle
    where [1] <> [2]


    With this script,I got result as below:



    +-----------+--------------+------------------+----------------------+----------------------+
    | reqid | idno | colName | From_Value | To_Value |
    +-----------+--------------+------------------+----------------------+----------------------+
    | 170916258 | 050505050505 | empname | | YOLO |
    | 170916258 | 050505050505 | emptyp | | 113 |
    | 170916258 | 050505050505 | gender | P | |
    | 170916258 | 050505050505 | income | 8891126 | |
    | 170916258 | 050505050505 | name | TEST2 | TEST3_2 |
    | 170916258 | 100202025698 | gender | L | P |
    | 170916258 | 100202025698 | industrialsector | 01261 | |
    | 170916258 | 100202025698 | name | APPLICANT5_2 | CUST3_6 |
    | 170916258 | 260404045698 | gender | | P |
    | 170916258 | 260404045698 | industrialsector | | 01279 |
    | 170916258 | 260404045698 | name | CUST4_3 | EHH4_4 |
    +-----------+--------------+------------------+----------------------+----------------------+


    How can I get result like below:



    +-----------+---------+--------------+------------------+--------------+----------+
    | reqid | Name | idno | colName | From_Value | To_Value |
    +-----------+---------+--------------+------------------+--------------+----------+
    | 170916258 | TEST3_2 | 50505050505 | empname | | YOLO |
    | 170916258 | TEST3_2 | 50505050505 | emptyp | | 113 |
    | 170916258 | TEST3_2 | 50505050505 | gender | P | |
    | 170916258 | TEST3_2 | 50505050505 | income | 8891126 | |
    | 170916258 | TEST3_2 | 50505050505 | name | TEST2 | TEST3_2 |
    | 170916258 | EHH4_4 | 100202025698 | gender | L | P |
    | 170916258 | EHH4_4 | 100202025698 | industrialsector | 1261 | |
    | 170916258 | EHH4_4 | 100202025698 | name | APPLICANT5_2 | CUST3_6 |
    | 170916258 | EHH4_4 | 260404045698 | gender | | P |
    | 170916258 | EHH4_4 | 260404045698 | industrialsector | | 1279 |
    | 170916258 | EHH4_4 | 260404045698 | name | CUST4_3 | EHH4_4 |
    +-----------+---------+--------------+------------------+--------------+----------+


    I want additional column "NAME". Any idea how to enhance this script?









    share









    New contributor




    user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      ;with #unpivot_step2 as
      (
      select reqid, Idno, RowNum, colName, Vals
      From
      (
      select
      cast(reqid as varchar(150)) AS reqid,
      cast(name collate database_default as varchar (150)) as name,
      cast(idno collate database_default as varchar (150)) as idno,
      cast(basicgroup collate database_default as varchar (150)) as basicgroup,
      cast(industrialsector collate database_default as varchar (150)) as industrialsector,
      cast(gender collate database_default as varchar (150)) as gender,
      cast(income collate database_default as varchar (150)) as income,
      cast(empname collate database_default as varchar(150)) as empname,
      cast(emptyp collate database_default as varchar (150)) as emptyp,
      row_number() over (partition by idno order by cast(date_created as datetime) desc) as RowNum
      from #mydatabase
      ) unpivot_table
      unpivot
      (
      vals for colName in (name, basicgroup, industrialsector, gender, income, empname, emptyp)
      ) unpivot_handle
      )

      select reqid, idno, ColName, [2] as [From], [1] as [To]
      FROM
      (
      select reqid, idno, rownum, colname, vals
      from #unpivot_step2
      ) pivot_table
      pivot
      (
      max (vals) for RowNum in ([1],[2])
      ) pivot_handle
      where [1] <> [2]


      With this script,I got result as below:



      +-----------+--------------+------------------+----------------------+----------------------+
      | reqid | idno | colName | From_Value | To_Value |
      +-----------+--------------+------------------+----------------------+----------------------+
      | 170916258 | 050505050505 | empname | | YOLO |
      | 170916258 | 050505050505 | emptyp | | 113 |
      | 170916258 | 050505050505 | gender | P | |
      | 170916258 | 050505050505 | income | 8891126 | |
      | 170916258 | 050505050505 | name | TEST2 | TEST3_2 |
      | 170916258 | 100202025698 | gender | L | P |
      | 170916258 | 100202025698 | industrialsector | 01261 | |
      | 170916258 | 100202025698 | name | APPLICANT5_2 | CUST3_6 |
      | 170916258 | 260404045698 | gender | | P |
      | 170916258 | 260404045698 | industrialsector | | 01279 |
      | 170916258 | 260404045698 | name | CUST4_3 | EHH4_4 |
      +-----------+--------------+------------------+----------------------+----------------------+


      How can I get result like below:



      +-----------+---------+--------------+------------------+--------------+----------+
      | reqid | Name | idno | colName | From_Value | To_Value |
      +-----------+---------+--------------+------------------+--------------+----------+
      | 170916258 | TEST3_2 | 50505050505 | empname | | YOLO |
      | 170916258 | TEST3_2 | 50505050505 | emptyp | | 113 |
      | 170916258 | TEST3_2 | 50505050505 | gender | P | |
      | 170916258 | TEST3_2 | 50505050505 | income | 8891126 | |
      | 170916258 | TEST3_2 | 50505050505 | name | TEST2 | TEST3_2 |
      | 170916258 | EHH4_4 | 100202025698 | gender | L | P |
      | 170916258 | EHH4_4 | 100202025698 | industrialsector | 1261 | |
      | 170916258 | EHH4_4 | 100202025698 | name | APPLICANT5_2 | CUST3_6 |
      | 170916258 | EHH4_4 | 260404045698 | gender | | P |
      | 170916258 | EHH4_4 | 260404045698 | industrialsector | | 1279 |
      | 170916258 | EHH4_4 | 260404045698 | name | CUST4_3 | EHH4_4 |
      +-----------+---------+--------------+------------------+--------------+----------+


      I want additional column "NAME". Any idea how to enhance this script?









      share









      New contributor




      user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      ;with #unpivot_step2 as
      (
      select reqid, Idno, RowNum, colName, Vals
      From
      (
      select
      cast(reqid as varchar(150)) AS reqid,
      cast(name collate database_default as varchar (150)) as name,
      cast(idno collate database_default as varchar (150)) as idno,
      cast(basicgroup collate database_default as varchar (150)) as basicgroup,
      cast(industrialsector collate database_default as varchar (150)) as industrialsector,
      cast(gender collate database_default as varchar (150)) as gender,
      cast(income collate database_default as varchar (150)) as income,
      cast(empname collate database_default as varchar(150)) as empname,
      cast(emptyp collate database_default as varchar (150)) as emptyp,
      row_number() over (partition by idno order by cast(date_created as datetime) desc) as RowNum
      from #mydatabase
      ) unpivot_table
      unpivot
      (
      vals for colName in (name, basicgroup, industrialsector, gender, income, empname, emptyp)
      ) unpivot_handle
      )

      select reqid, idno, ColName, [2] as [From], [1] as [To]
      FROM
      (
      select reqid, idno, rownum, colname, vals
      from #unpivot_step2
      ) pivot_table
      pivot
      (
      max (vals) for RowNum in ([1],[2])
      ) pivot_handle
      where [1] <> [2]


      With this script,I got result as below:



      +-----------+--------------+------------------+----------------------+----------------------+
      | reqid | idno | colName | From_Value | To_Value |
      +-----------+--------------+------------------+----------------------+----------------------+
      | 170916258 | 050505050505 | empname | | YOLO |
      | 170916258 | 050505050505 | emptyp | | 113 |
      | 170916258 | 050505050505 | gender | P | |
      | 170916258 | 050505050505 | income | 8891126 | |
      | 170916258 | 050505050505 | name | TEST2 | TEST3_2 |
      | 170916258 | 100202025698 | gender | L | P |
      | 170916258 | 100202025698 | industrialsector | 01261 | |
      | 170916258 | 100202025698 | name | APPLICANT5_2 | CUST3_6 |
      | 170916258 | 260404045698 | gender | | P |
      | 170916258 | 260404045698 | industrialsector | | 01279 |
      | 170916258 | 260404045698 | name | CUST4_3 | EHH4_4 |
      +-----------+--------------+------------------+----------------------+----------------------+


      How can I get result like below:



      +-----------+---------+--------------+------------------+--------------+----------+
      | reqid | Name | idno | colName | From_Value | To_Value |
      +-----------+---------+--------------+------------------+--------------+----------+
      | 170916258 | TEST3_2 | 50505050505 | empname | | YOLO |
      | 170916258 | TEST3_2 | 50505050505 | emptyp | | 113 |
      | 170916258 | TEST3_2 | 50505050505 | gender | P | |
      | 170916258 | TEST3_2 | 50505050505 | income | 8891126 | |
      | 170916258 | TEST3_2 | 50505050505 | name | TEST2 | TEST3_2 |
      | 170916258 | EHH4_4 | 100202025698 | gender | L | P |
      | 170916258 | EHH4_4 | 100202025698 | industrialsector | 1261 | |
      | 170916258 | EHH4_4 | 100202025698 | name | APPLICANT5_2 | CUST3_6 |
      | 170916258 | EHH4_4 | 260404045698 | gender | | P |
      | 170916258 | EHH4_4 | 260404045698 | industrialsector | | 1279 |
      | 170916258 | EHH4_4 | 260404045698 | name | CUST4_3 | EHH4_4 |
      +-----------+---------+--------------+------------------+--------------+----------+


      I want additional column "NAME". Any idea how to enhance this script?







      sql-server sql-server-2012 sql-server-2014





      share









      New contributor




      user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share









      New contributor




      user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share








      edited 3 mins ago







      user3542587













      New contributor




      user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 9 mins ago









      user3542587user3542587

      11




      11




      New contributor




      user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user3542587 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          user3542587 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f231114%2fadditional-column-in-unpivot-pivot-in-mssql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          user3542587 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          user3542587 is a new contributor. Be nice, and check out our Code of Conduct.













          user3542587 is a new contributor. Be nice, and check out our Code of Conduct.












          user3542587 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f231114%2fadditional-column-in-unpivot-pivot-in-mssql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

          Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

          How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...