kdb+/q Pivot Table to Turn Multiple Columns into a Single Column?
Given the following table:
date acct sec qtya qtyb qtyc
----------------------------
11 1 xx 10 60 110
22 2 yy 20 70 120
33 3 zz 30 80 130
44 2 xx 40 90 140
55 3 yy 50 100 150
Created using this statement:
t:(date:11 22 33 44 55;acct:1 2 3 2 3;sec:`xx`yy`zz`xx`yy;qtya:10 20 30 40 50;qtyb:60 70 80 90 100;qtyc:110 120 130 140 150)
Give a way to return the data as follows:
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
Which can be created using this statement:
q)T:(date:11 11 11 22 22 22 33 33 33 44 44 44 55 55 55;acct:1 1 1 2 2 2 3 3 3 2 2 2 3 3 3;sec:`xx`xx`xx`yy`yy`yy`zz`zz`zz`xx`xx`xx`yy`yy`yy;qty:10 60 110 20 70 120 30 80 130 40 90 140 50 100 150)
kdb
add a comment |
Given the following table:
date acct sec qtya qtyb qtyc
----------------------------
11 1 xx 10 60 110
22 2 yy 20 70 120
33 3 zz 30 80 130
44 2 xx 40 90 140
55 3 yy 50 100 150
Created using this statement:
t:(date:11 22 33 44 55;acct:1 2 3 2 3;sec:`xx`yy`zz`xx`yy;qtya:10 20 30 40 50;qtyb:60 70 80 90 100;qtyc:110 120 130 140 150)
Give a way to return the data as follows:
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
Which can be created using this statement:
q)T:(date:11 11 11 22 22 22 33 33 33 44 44 44 55 55 55;acct:1 1 1 2 2 2 3 3 3 2 2 2 3 3 3;sec:`xx`xx`xx`yy`yy`yy`zz`zz`zz`xx`xx`xx`yy`yy`yy;qty:10 60 110 20 70 120 30 80 130 40 90 140 50 100 150)
kdb
add a comment |
Given the following table:
date acct sec qtya qtyb qtyc
----------------------------
11 1 xx 10 60 110
22 2 yy 20 70 120
33 3 zz 30 80 130
44 2 xx 40 90 140
55 3 yy 50 100 150
Created using this statement:
t:(date:11 22 33 44 55;acct:1 2 3 2 3;sec:`xx`yy`zz`xx`yy;qtya:10 20 30 40 50;qtyb:60 70 80 90 100;qtyc:110 120 130 140 150)
Give a way to return the data as follows:
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
Which can be created using this statement:
q)T:(date:11 11 11 22 22 22 33 33 33 44 44 44 55 55 55;acct:1 1 1 2 2 2 3 3 3 2 2 2 3 3 3;sec:`xx`xx`xx`yy`yy`yy`zz`zz`zz`xx`xx`xx`yy`yy`yy;qty:10 60 110 20 70 120 30 80 130 40 90 140 50 100 150)
kdb
Given the following table:
date acct sec qtya qtyb qtyc
----------------------------
11 1 xx 10 60 110
22 2 yy 20 70 120
33 3 zz 30 80 130
44 2 xx 40 90 140
55 3 yy 50 100 150
Created using this statement:
t:(date:11 22 33 44 55;acct:1 2 3 2 3;sec:`xx`yy`zz`xx`yy;qtya:10 20 30 40 50;qtyb:60 70 80 90 100;qtyc:110 120 130 140 150)
Give a way to return the data as follows:
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
Which can be created using this statement:
q)T:(date:11 11 11 22 22 22 33 33 33 44 44 44 55 55 55;acct:1 1 1 2 2 2 3 3 3 2 2 2 3 3 3;sec:`xx`xx`xx`yy`yy`yy`zz`zz`zz`xx`xx`xx`yy`yy`yy;qty:10 60 110 20 70 120 30 80 130 40 90 140 50 100 150)
kdb
kdb
edited Nov 14 '18 at 21:41
user2242865
497412
497412
asked Nov 14 '18 at 4:07
UllauriUllauri
347510
347510
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Using flip
:
q)ungroup select date , acct, sec , qty: flip (qtya;qtyb;qtyc) from t
add a comment |
q)ungroup 3!select date,acct,sec,qty:(qtya,'qtyb,'qtyc) from t
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);
);
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%2fstackoverflow.com%2fquestions%2f53293058%2fkdb-q-pivot-table-to-turn-multiple-columns-into-a-single-column%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using flip
:
q)ungroup select date , acct, sec , qty: flip (qtya;qtyb;qtyc) from t
add a comment |
Using flip
:
q)ungroup select date , acct, sec , qty: flip (qtya;qtyb;qtyc) from t
add a comment |
Using flip
:
q)ungroup select date , acct, sec , qty: flip (qtya;qtyb;qtyc) from t
Using flip
:
q)ungroup select date , acct, sec , qty: flip (qtya;qtyb;qtyc) from t
answered Nov 14 '18 at 7:54
aliakaliak
5,88531436
5,88531436
add a comment |
add a comment |
q)ungroup 3!select date,acct,sec,qty:(qtya,'qtyb,'qtyc) from t
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
add a comment |
q)ungroup 3!select date,acct,sec,qty:(qtya,'qtyb,'qtyc) from t
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
add a comment |
q)ungroup 3!select date,acct,sec,qty:(qtya,'qtyb,'qtyc) from t
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
q)ungroup 3!select date,acct,sec,qty:(qtya,'qtyb,'qtyc) from t
date acct sec qty
-----------------
11 1 xx 10
11 1 xx 60
11 1 xx 110
22 2 yy 20
22 2 yy 70
22 2 yy 120
33 3 zz 30
33 3 zz 80
33 3 zz 130
44 2 xx 40
44 2 xx 90
44 2 xx 140
55 3 yy 50
55 3 yy 100
55 3 yy 150
answered Nov 14 '18 at 4:13
user2242865user2242865
497412
497412
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53293058%2fkdb-q-pivot-table-to-turn-multiple-columns-into-a-single-column%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