Additional column in unpivot/pivot in MSSQLPassing column names dynamically to UNPIVOTPivot column...
Is a debit card dangerous for an account with low balance and no overdraft protection?
Can you earn endless XP using a Flameskull and its self-revival feature?
What to do when being responsible for data protection in your lab, yet advice is ignored?
Quenching swords in dragon blood; why?
Compress command output by piping to bzip2
If I delete my router's history can my ISP still provide it to my parents?
Eww, those bytes are gross
How to avoid being sexist when trying to employ someone to function in a very sexist environment?
How do I say "Brexit" in Latin?
How would one buy a used TIE Fighter or X-Wing?
Cryptic with missing capitals
Can a hotel cancel a confirmed reservation?
Difference between two quite-similar Terminal commands
Parsing a string of key-value pairs as a dictionary
It took me a lot of time to make this, pls like. (YouTube Comments #1)
How to explain planetary rings pulsating?
How to tag distinct options/entities without giving any an implicit priority or suggested order?
What is better: yes / no radio, or simple checkbox?
A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?
Why did other German political parties disband so fast when Hitler was appointed chancellor?
What's a good word to describe a public place that looks like it wouldn't be rough?
Why are the books in the Game of Thrones citadel library shelved spine inwards?
What is the most triangles you can make from a capital "H" and 3 straight lines?
Placing an adverb between a verb and an object?
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
;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
New contributor
add a comment |
;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
New contributor
add a comment |
;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
New contributor
;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
sql-server sql-server-2012 sql-server-2014
New contributor
New contributor
edited 2 mins ago
user3542587
New contributor
asked 8 mins ago
user3542587user3542587
11
11
New contributor
New contributor
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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