Join one value from an array to each result row












0















I want to select data from a table and assign one value from an array to each result row.



e.g. given a table with the following product data



product
-------
car
bike
elephant


and an array like this



string_to_array('0.8, 0.6, 0.9', ',')


I want to get a table like this



product | value
---------------
bike 0.8
car 0.6
elephant 0.9


Any ideas how to easily solve this?



Something like this



WITH data_table AS (
SELECT
pr.id
, pr.name
FROM
products pr
ORDER BY pr.id
LIMIT 20
)
SELECT
data_table.name
, multipliers[row_number() over(order by data_table.id)]::numeric * data_table.id AS multiplied_value
FROM
data_table
, string_to_array('0.8, 0.6, 0.9, 0.65, 0.34, 0.67, 0.85, 0.12, 0.45, 0.76, 0.68, 0.98, 1.23, 0.69, 0.86, 1.11, 0.75, 1.02, 0.3, 0.79', ',') multipliers
ORDER BY multiplied_value DESC
LIMIT 20









share|improve this question

























  • Can you be more specific please?

    – Kurohige
    Nov 22 '18 at 20:12











  • What is the assign logic? You can update your table, or you have to solve with sql select. You can solve it out of postgre too.

    – László Tóth
    Nov 22 '18 at 20:25
















0















I want to select data from a table and assign one value from an array to each result row.



e.g. given a table with the following product data



product
-------
car
bike
elephant


and an array like this



string_to_array('0.8, 0.6, 0.9', ',')


I want to get a table like this



product | value
---------------
bike 0.8
car 0.6
elephant 0.9


Any ideas how to easily solve this?



Something like this



WITH data_table AS (
SELECT
pr.id
, pr.name
FROM
products pr
ORDER BY pr.id
LIMIT 20
)
SELECT
data_table.name
, multipliers[row_number() over(order by data_table.id)]::numeric * data_table.id AS multiplied_value
FROM
data_table
, string_to_array('0.8, 0.6, 0.9, 0.65, 0.34, 0.67, 0.85, 0.12, 0.45, 0.76, 0.68, 0.98, 1.23, 0.69, 0.86, 1.11, 0.75, 1.02, 0.3, 0.79', ',') multipliers
ORDER BY multiplied_value DESC
LIMIT 20









share|improve this question

























  • Can you be more specific please?

    – Kurohige
    Nov 22 '18 at 20:12











  • What is the assign logic? You can update your table, or you have to solve with sql select. You can solve it out of postgre too.

    – László Tóth
    Nov 22 '18 at 20:25














0












0








0








I want to select data from a table and assign one value from an array to each result row.



e.g. given a table with the following product data



product
-------
car
bike
elephant


and an array like this



string_to_array('0.8, 0.6, 0.9', ',')


I want to get a table like this



product | value
---------------
bike 0.8
car 0.6
elephant 0.9


Any ideas how to easily solve this?



Something like this



WITH data_table AS (
SELECT
pr.id
, pr.name
FROM
products pr
ORDER BY pr.id
LIMIT 20
)
SELECT
data_table.name
, multipliers[row_number() over(order by data_table.id)]::numeric * data_table.id AS multiplied_value
FROM
data_table
, string_to_array('0.8, 0.6, 0.9, 0.65, 0.34, 0.67, 0.85, 0.12, 0.45, 0.76, 0.68, 0.98, 1.23, 0.69, 0.86, 1.11, 0.75, 1.02, 0.3, 0.79', ',') multipliers
ORDER BY multiplied_value DESC
LIMIT 20









share|improve this question
















I want to select data from a table and assign one value from an array to each result row.



e.g. given a table with the following product data



product
-------
car
bike
elephant


and an array like this



string_to_array('0.8, 0.6, 0.9', ',')


I want to get a table like this



product | value
---------------
bike 0.8
car 0.6
elephant 0.9


Any ideas how to easily solve this?



Something like this



WITH data_table AS (
SELECT
pr.id
, pr.name
FROM
products pr
ORDER BY pr.id
LIMIT 20
)
SELECT
data_table.name
, multipliers[row_number() over(order by data_table.id)]::numeric * data_table.id AS multiplied_value
FROM
data_table
, string_to_array('0.8, 0.6, 0.9, 0.65, 0.34, 0.67, 0.85, 0.12, 0.45, 0.76, 0.68, 0.98, 1.23, 0.69, 0.86, 1.11, 0.75, 1.02, 0.3, 0.79', ',') multipliers
ORDER BY multiplied_value DESC
LIMIT 20






postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 20:28







Joseph Tura

















asked Nov 22 '18 at 20:07









Joseph TuraJoseph Tura

4,04653767




4,04653767













  • Can you be more specific please?

    – Kurohige
    Nov 22 '18 at 20:12











  • What is the assign logic? You can update your table, or you have to solve with sql select. You can solve it out of postgre too.

    – László Tóth
    Nov 22 '18 at 20:25



















  • Can you be more specific please?

    – Kurohige
    Nov 22 '18 at 20:12











  • What is the assign logic? You can update your table, or you have to solve with sql select. You can solve it out of postgre too.

    – László Tóth
    Nov 22 '18 at 20:25

















Can you be more specific please?

– Kurohige
Nov 22 '18 at 20:12





Can you be more specific please?

– Kurohige
Nov 22 '18 at 20:12













What is the assign logic? You can update your table, or you have to solve with sql select. You can solve it out of postgre too.

– László Tóth
Nov 22 '18 at 20:25





What is the assign logic? You can update your table, or you have to solve with sql select. You can solve it out of postgre too.

– László Tóth
Nov 22 '18 at 20:25












1 Answer
1






active

oldest

votes


















0














Assuming the order of the rows should be defined by sorting by name, you can use the following:



select name, n[rn] as value
from (
select name, row_number() over (order by name) as rn,
string_to_array('0.8, 0.6, 0.9', ',') as n
from the_table
) t





share|improve this answer
























  • I think my problem is that I want to order the end result differently from the sort order for determining the row numbers. In the above extended example, I want to sort by the result of the multiplication of the array value with a value in the table.

    – Joseph Tura
    Nov 22 '18 at 20:38











  • Actually both your solution and my solution above seem to work fine. Just didn't see it after staring at the screen for too long.

    – Joseph Tura
    Nov 22 '18 at 20:44











  • @JosephTura: you can sort the result of my query by any column you like

    – a_horse_with_no_name
    Nov 22 '18 at 21:14











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437483%2fjoin-one-value-from-an-array-to-each-result-row%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Assuming the order of the rows should be defined by sorting by name, you can use the following:



select name, n[rn] as value
from (
select name, row_number() over (order by name) as rn,
string_to_array('0.8, 0.6, 0.9', ',') as n
from the_table
) t





share|improve this answer
























  • I think my problem is that I want to order the end result differently from the sort order for determining the row numbers. In the above extended example, I want to sort by the result of the multiplication of the array value with a value in the table.

    – Joseph Tura
    Nov 22 '18 at 20:38











  • Actually both your solution and my solution above seem to work fine. Just didn't see it after staring at the screen for too long.

    – Joseph Tura
    Nov 22 '18 at 20:44











  • @JosephTura: you can sort the result of my query by any column you like

    – a_horse_with_no_name
    Nov 22 '18 at 21:14
















0














Assuming the order of the rows should be defined by sorting by name, you can use the following:



select name, n[rn] as value
from (
select name, row_number() over (order by name) as rn,
string_to_array('0.8, 0.6, 0.9', ',') as n
from the_table
) t





share|improve this answer
























  • I think my problem is that I want to order the end result differently from the sort order for determining the row numbers. In the above extended example, I want to sort by the result of the multiplication of the array value with a value in the table.

    – Joseph Tura
    Nov 22 '18 at 20:38











  • Actually both your solution and my solution above seem to work fine. Just didn't see it after staring at the screen for too long.

    – Joseph Tura
    Nov 22 '18 at 20:44











  • @JosephTura: you can sort the result of my query by any column you like

    – a_horse_with_no_name
    Nov 22 '18 at 21:14














0












0








0







Assuming the order of the rows should be defined by sorting by name, you can use the following:



select name, n[rn] as value
from (
select name, row_number() over (order by name) as rn,
string_to_array('0.8, 0.6, 0.9', ',') as n
from the_table
) t





share|improve this answer













Assuming the order of the rows should be defined by sorting by name, you can use the following:



select name, n[rn] as value
from (
select name, row_number() over (order by name) as rn,
string_to_array('0.8, 0.6, 0.9', ',') as n
from the_table
) t






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 20:27









a_horse_with_no_namea_horse_with_no_name

295k46451546




295k46451546













  • I think my problem is that I want to order the end result differently from the sort order for determining the row numbers. In the above extended example, I want to sort by the result of the multiplication of the array value with a value in the table.

    – Joseph Tura
    Nov 22 '18 at 20:38











  • Actually both your solution and my solution above seem to work fine. Just didn't see it after staring at the screen for too long.

    – Joseph Tura
    Nov 22 '18 at 20:44











  • @JosephTura: you can sort the result of my query by any column you like

    – a_horse_with_no_name
    Nov 22 '18 at 21:14



















  • I think my problem is that I want to order the end result differently from the sort order for determining the row numbers. In the above extended example, I want to sort by the result of the multiplication of the array value with a value in the table.

    – Joseph Tura
    Nov 22 '18 at 20:38











  • Actually both your solution and my solution above seem to work fine. Just didn't see it after staring at the screen for too long.

    – Joseph Tura
    Nov 22 '18 at 20:44











  • @JosephTura: you can sort the result of my query by any column you like

    – a_horse_with_no_name
    Nov 22 '18 at 21:14

















I think my problem is that I want to order the end result differently from the sort order for determining the row numbers. In the above extended example, I want to sort by the result of the multiplication of the array value with a value in the table.

– Joseph Tura
Nov 22 '18 at 20:38





I think my problem is that I want to order the end result differently from the sort order for determining the row numbers. In the above extended example, I want to sort by the result of the multiplication of the array value with a value in the table.

– Joseph Tura
Nov 22 '18 at 20:38













Actually both your solution and my solution above seem to work fine. Just didn't see it after staring at the screen for too long.

– Joseph Tura
Nov 22 '18 at 20:44





Actually both your solution and my solution above seem to work fine. Just didn't see it after staring at the screen for too long.

– Joseph Tura
Nov 22 '18 at 20:44













@JosephTura: you can sort the result of my query by any column you like

– a_horse_with_no_name
Nov 22 '18 at 21:14





@JosephTura: you can sort the result of my query by any column you like

– a_horse_with_no_name
Nov 22 '18 at 21:14


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437483%2fjoin-one-value-from-an-array-to-each-result-row%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

Futebolista

Feedback on college project

Albești (Vaslui)