Postgresql to pivot











up vote
1
down vote

favorite












I have a requirement like below in postgresql.



ID       URL  
1 www.google.com
1 www.yahoo.com
1 www.hotmail.com


I want the output to be like



ID       URL1           URL2          URL3  
1 www.google.com www.yahoo.com www.hotmail.com


I tried crosstab and I am getting an error SQL Error [42883]: ERROR: function crosstab(unknown) does not exist in Metabase and dbeaver. Can someone help with a SQL.



select id, case when rn = 1 then url end url1 , 
case when rn = 2 then url end url2
from (
SELECT *, row_number() over (partition by id) rn
FROM table1
where url is not null)start1


but its not as expected.










share|improve this question




















  • 2




    You need to install the tablefunc extension
    – a_horse_with_no_name
    Nov 8 at 10:25















up vote
1
down vote

favorite












I have a requirement like below in postgresql.



ID       URL  
1 www.google.com
1 www.yahoo.com
1 www.hotmail.com


I want the output to be like



ID       URL1           URL2          URL3  
1 www.google.com www.yahoo.com www.hotmail.com


I tried crosstab and I am getting an error SQL Error [42883]: ERROR: function crosstab(unknown) does not exist in Metabase and dbeaver. Can someone help with a SQL.



select id, case when rn = 1 then url end url1 , 
case when rn = 2 then url end url2
from (
SELECT *, row_number() over (partition by id) rn
FROM table1
where url is not null)start1


but its not as expected.










share|improve this question




















  • 2




    You need to install the tablefunc extension
    – a_horse_with_no_name
    Nov 8 at 10:25













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a requirement like below in postgresql.



ID       URL  
1 www.google.com
1 www.yahoo.com
1 www.hotmail.com


I want the output to be like



ID       URL1           URL2          URL3  
1 www.google.com www.yahoo.com www.hotmail.com


I tried crosstab and I am getting an error SQL Error [42883]: ERROR: function crosstab(unknown) does not exist in Metabase and dbeaver. Can someone help with a SQL.



select id, case when rn = 1 then url end url1 , 
case when rn = 2 then url end url2
from (
SELECT *, row_number() over (partition by id) rn
FROM table1
where url is not null)start1


but its not as expected.










share|improve this question















I have a requirement like below in postgresql.



ID       URL  
1 www.google.com
1 www.yahoo.com
1 www.hotmail.com


I want the output to be like



ID       URL1           URL2          URL3  
1 www.google.com www.yahoo.com www.hotmail.com


I tried crosstab and I am getting an error SQL Error [42883]: ERROR: function crosstab(unknown) does not exist in Metabase and dbeaver. Can someone help with a SQL.



select id, case when rn = 1 then url end url1 , 
case when rn = 2 then url end url2
from (
SELECT *, row_number() over (partition by id) rn
FROM table1
where url is not null)start1


but its not as expected.







postgresql crosstab






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 10:24









a_horse_with_no_name

285k45427525




285k45427525










asked Nov 8 at 10:08









George

112




112








  • 2




    You need to install the tablefunc extension
    – a_horse_with_no_name
    Nov 8 at 10:25














  • 2




    You need to install the tablefunc extension
    – a_horse_with_no_name
    Nov 8 at 10:25








2




2




You need to install the tablefunc extension
– a_horse_with_no_name
Nov 8 at 10:25




You need to install the tablefunc extension
– a_horse_with_no_name
Nov 8 at 10:25

















active

oldest

votes











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205491%2fpostgresql-to-pivot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205491%2fpostgresql-to-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

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff