Additional column in unpivot/pivot












0















;with #unpivot_step2 as
(
select reqid, Idno, RowNum, Item, 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 Item in (name, basicgroup, industrialsector, gender, income, empname, emptyp)
) unpivot_handle
)

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


With this script,I got as below result.



+-----------+--------------+------------------+----------------------+----------------------+
| 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.





















  • I want get 2 latest result to do comparison based on date and group by idno Add ROW_NUMBER() OVER (PARTITION BY IdNo ORDER BY date DESC). Take records where it is 1 or 2. Compare.

    – Akina
    19 hours ago













  • SQL Create Table/Insert Script It is wrong. 1) seqno .. PRIMARY KEY whereas you insert duplicates; 2) idno INTEGER whereas you insert values which needs at least 40 bit.

    – Akina
    19 hours ago











  • @Akina 1) seq_no it is a primary key, but the result need it duplicate as want to track this 1 application has how many people applying it and how frequent for changes. 2) i changed it to varchar(12), limit 12 digit only.

    – user3542587
    19 hours ago











  • seq_no it is a primary key, but the result What does the result? Look at your source data! Or at least try to execute your "SQL Create Table/Insert Script" on your server...

    – Akina
    18 hours ago






  • 1





    it is not very clear what you are asking for, besides the picture, if I need to put time to answer this, which is a pleasure for me, but at least I need to know more details, all the details in fact, to produce the desired result. After all, I am volunteering here.

    – marcello miorelli
    15 hours ago
















0















;with #unpivot_step2 as
(
select reqid, Idno, RowNum, Item, 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 Item in (name, basicgroup, industrialsector, gender, income, empname, emptyp)
) unpivot_handle
)

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


With this script,I got as below result.



+-----------+--------------+------------------+----------------------+----------------------+
| 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.





















  • I want get 2 latest result to do comparison based on date and group by idno Add ROW_NUMBER() OVER (PARTITION BY IdNo ORDER BY date DESC). Take records where it is 1 or 2. Compare.

    – Akina
    19 hours ago













  • SQL Create Table/Insert Script It is wrong. 1) seqno .. PRIMARY KEY whereas you insert duplicates; 2) idno INTEGER whereas you insert values which needs at least 40 bit.

    – Akina
    19 hours ago











  • @Akina 1) seq_no it is a primary key, but the result need it duplicate as want to track this 1 application has how many people applying it and how frequent for changes. 2) i changed it to varchar(12), limit 12 digit only.

    – user3542587
    19 hours ago











  • seq_no it is a primary key, but the result What does the result? Look at your source data! Or at least try to execute your "SQL Create Table/Insert Script" on your server...

    – Akina
    18 hours ago






  • 1





    it is not very clear what you are asking for, besides the picture, if I need to put time to answer this, which is a pleasure for me, but at least I need to know more details, all the details in fact, to produce the desired result. After all, I am volunteering here.

    – marcello miorelli
    15 hours ago














0












0








0








;with #unpivot_step2 as
(
select reqid, Idno, RowNum, Item, 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 Item in (name, basicgroup, industrialsector, gender, income, empname, emptyp)
) unpivot_handle
)

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


With this script,I got as below result.



+-----------+--------------+------------------+----------------------+----------------------+
| 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, Item, 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 Item in (name, basicgroup, industrialsector, gender, income, empname, emptyp)
) unpivot_handle
)

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


With this script,I got as below result.



+-----------+--------------+------------------+----------------------+----------------------+
| 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






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 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 19 hours 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.













  • I want get 2 latest result to do comparison based on date and group by idno Add ROW_NUMBER() OVER (PARTITION BY IdNo ORDER BY date DESC). Take records where it is 1 or 2. Compare.

    – Akina
    19 hours ago













  • SQL Create Table/Insert Script It is wrong. 1) seqno .. PRIMARY KEY whereas you insert duplicates; 2) idno INTEGER whereas you insert values which needs at least 40 bit.

    – Akina
    19 hours ago











  • @Akina 1) seq_no it is a primary key, but the result need it duplicate as want to track this 1 application has how many people applying it and how frequent for changes. 2) i changed it to varchar(12), limit 12 digit only.

    – user3542587
    19 hours ago











  • seq_no it is a primary key, but the result What does the result? Look at your source data! Or at least try to execute your "SQL Create Table/Insert Script" on your server...

    – Akina
    18 hours ago






  • 1





    it is not very clear what you are asking for, besides the picture, if I need to put time to answer this, which is a pleasure for me, but at least I need to know more details, all the details in fact, to produce the desired result. After all, I am volunteering here.

    – marcello miorelli
    15 hours ago



















  • I want get 2 latest result to do comparison based on date and group by idno Add ROW_NUMBER() OVER (PARTITION BY IdNo ORDER BY date DESC). Take records where it is 1 or 2. Compare.

    – Akina
    19 hours ago













  • SQL Create Table/Insert Script It is wrong. 1) seqno .. PRIMARY KEY whereas you insert duplicates; 2) idno INTEGER whereas you insert values which needs at least 40 bit.

    – Akina
    19 hours ago











  • @Akina 1) seq_no it is a primary key, but the result need it duplicate as want to track this 1 application has how many people applying it and how frequent for changes. 2) i changed it to varchar(12), limit 12 digit only.

    – user3542587
    19 hours ago











  • seq_no it is a primary key, but the result What does the result? Look at your source data! Or at least try to execute your "SQL Create Table/Insert Script" on your server...

    – Akina
    18 hours ago






  • 1





    it is not very clear what you are asking for, besides the picture, if I need to put time to answer this, which is a pleasure for me, but at least I need to know more details, all the details in fact, to produce the desired result. After all, I am volunteering here.

    – marcello miorelli
    15 hours ago

















I want get 2 latest result to do comparison based on date and group by idno Add ROW_NUMBER() OVER (PARTITION BY IdNo ORDER BY date DESC). Take records where it is 1 or 2. Compare.

– Akina
19 hours ago







I want get 2 latest result to do comparison based on date and group by idno Add ROW_NUMBER() OVER (PARTITION BY IdNo ORDER BY date DESC). Take records where it is 1 or 2. Compare.

– Akina
19 hours ago















SQL Create Table/Insert Script It is wrong. 1) seqno .. PRIMARY KEY whereas you insert duplicates; 2) idno INTEGER whereas you insert values which needs at least 40 bit.

– Akina
19 hours ago





SQL Create Table/Insert Script It is wrong. 1) seqno .. PRIMARY KEY whereas you insert duplicates; 2) idno INTEGER whereas you insert values which needs at least 40 bit.

– Akina
19 hours ago













@Akina 1) seq_no it is a primary key, but the result need it duplicate as want to track this 1 application has how many people applying it and how frequent for changes. 2) i changed it to varchar(12), limit 12 digit only.

– user3542587
19 hours ago





@Akina 1) seq_no it is a primary key, but the result need it duplicate as want to track this 1 application has how many people applying it and how frequent for changes. 2) i changed it to varchar(12), limit 12 digit only.

– user3542587
19 hours ago













seq_no it is a primary key, but the result What does the result? Look at your source data! Or at least try to execute your "SQL Create Table/Insert Script" on your server...

– Akina
18 hours ago





seq_no it is a primary key, but the result What does the result? Look at your source data! Or at least try to execute your "SQL Create Table/Insert Script" on your server...

– Akina
18 hours ago




1




1





it is not very clear what you are asking for, besides the picture, if I need to put time to answer this, which is a pleasure for me, but at least I need to know more details, all the details in fact, to produce the desired result. After all, I am volunteering here.

– marcello miorelli
15 hours ago





it is not very clear what you are asking for, besides the picture, if I need to put time to answer this, which is a pleasure for me, but at least I need to know more details, all the details in fact, to produce the desired result. After all, I am volunteering here.

– marcello miorelli
15 hours ago










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%2f231031%2fadditional-column-in-unpivot-pivot%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%2f231031%2fadditional-column-in-unpivot-pivot%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

الفوسفات في المغرب

Four equal circles intersect: What is the area of the small shaded portion and its height

بطل الاتحاد السوفيتي