Additional column in unpivot/pivot in MSSQL












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|improve this question









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|improve this question









    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|improve this question









      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|improve this question









      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|improve this question









      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|improve this question




      share|improve this question








      edited 4 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 10 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

          SQL Server 17 - Attemping to backup to remote NAS but Access is denied

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Restoring from pg_dump with foreign key constraints