Join one value from an array to each result row
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
add a comment |
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
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
add a comment |
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
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
postgresql
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
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
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%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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
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%2f53437483%2fjoin-one-value-from-an-array-to-each-result-row%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
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