Can I compress two rows with a difference in two columns into a single row with both columns?change display...
Why don't programs completely uninstall (remove all their files) when I remove them?
Is it possible to narrate a novel in a faux-historical style without alienating the reader?
Does しかたない imply disappointment?
What could cause an entire planet of humans to become aphasic?
What is formjacking?
Sets which are both Sum-free and Product-free.
Partial derivative with respect to three variables
Is the percentage symbol a constant?
In the Lost in Space intro why was Dr. Smith actor listed as a special guest star?
Why write a book when there's a movie in my head?
What if you do not believe in the project benefits?
How can I persuade an unwilling soul to become willing?
Why don't you get burned by the wood benches in a sauna?
How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?
Minimum Viable Product for RTS game?
How does holding onto an active but un-used credit card affect your ability to get a loan?
Does the double-bladed scimitar's special attack let you use your ability modifier for the damage of the attack?
How many copper coins fit inside a cubic foot?
How can I handle players killing my NPC outside of combat?
What does "don't have a baby" imply or mean in this sentence?
How can I make my enemies feel real and make combat more engaging?
Boss asked me to sign a resignation paper without a date on it along with my new contract
Including proofs of known theorems in master's thesis
Coworker asking me to not bring cakes due to self control issue. What should I do?
Can I compress two rows with a difference in two columns into a single row with both columns?
change display of rows into single row in SQL server 2008Postgresql assign mutliple row query result to variable and continue executionTransforming multiple rows into a single rowTwo rows into two columnsConsolidate multiple rows into single rowConvert Rows into ColumnsRow level security with a single DB user and connection poolingReturn sample row if result set is emptyPostgres 9.5 Materialized View from a table with jsonb columnsHow can I combine a large CROSS JOIN query and return 1 result?
I have a query that returns customer product information that shows which region a product is sourced from.
Query:
SELECT
prod.prod_id,
prod.is_source,
link.prod_link_disabled,
loc.location_id,
loc.customer_id,
cust.customer_name
FROM
product prod,
product_link link,
region region,
location loc,
customer cust
WHERE
prod.prod_id = link.prod_id
AND link.region_id = region.region_id
AND region.loc_id = loc.loc_id
AND link.prod_link_deleted = FALSE
AND loc.customer_id = customer_id
AND prod.prod_id = 5874;
This returns the following result:
| prod_id | is_source | prod_link_disabled | location_id | customer_id | customer_name |
| ------------------- | --------- | ------------------ | ----------- | ----------- | --------------- |
| Test Product - 5874 | t | f | 2 | 332 | Test Playground |
| Test Product - 5874 | f | f | 1 | 332 | Test Playground |
This is showing that product 5874 is coming from location 2 (because is_source
is true).
I'd like to compress this information into a single row. I'd like a result that looks like this:
| prod_id | prod_link_disabled | src_location_id | dest_location_id | customer_id | customer_name |
| ------------------- | ------------------ | --------------- | ---------------- | ----------- | --------------- |
| Test Product - 5874 | f | 2 | 1 | 332 | Test Playground |
This compressed the two previous rows into a single row where src_location_id
is 2
(again because that row had is_source
as true) and dest_location_id
is 1
How can I compress the two row result?
It is safe to assume that there are always only two results in the first query.
postgresql pivot row
New contributor
add a comment |
I have a query that returns customer product information that shows which region a product is sourced from.
Query:
SELECT
prod.prod_id,
prod.is_source,
link.prod_link_disabled,
loc.location_id,
loc.customer_id,
cust.customer_name
FROM
product prod,
product_link link,
region region,
location loc,
customer cust
WHERE
prod.prod_id = link.prod_id
AND link.region_id = region.region_id
AND region.loc_id = loc.loc_id
AND link.prod_link_deleted = FALSE
AND loc.customer_id = customer_id
AND prod.prod_id = 5874;
This returns the following result:
| prod_id | is_source | prod_link_disabled | location_id | customer_id | customer_name |
| ------------------- | --------- | ------------------ | ----------- | ----------- | --------------- |
| Test Product - 5874 | t | f | 2 | 332 | Test Playground |
| Test Product - 5874 | f | f | 1 | 332 | Test Playground |
This is showing that product 5874 is coming from location 2 (because is_source
is true).
I'd like to compress this information into a single row. I'd like a result that looks like this:
| prod_id | prod_link_disabled | src_location_id | dest_location_id | customer_id | customer_name |
| ------------------- | ------------------ | --------------- | ---------------- | ----------- | --------------- |
| Test Product - 5874 | f | 2 | 1 | 332 | Test Playground |
This compressed the two previous rows into a single row where src_location_id
is 2
(again because that row had is_source
as true) and dest_location_id
is 1
How can I compress the two row result?
It is safe to assume that there are always only two results in the first query.
postgresql pivot row
New contributor
add a comment |
I have a query that returns customer product information that shows which region a product is sourced from.
Query:
SELECT
prod.prod_id,
prod.is_source,
link.prod_link_disabled,
loc.location_id,
loc.customer_id,
cust.customer_name
FROM
product prod,
product_link link,
region region,
location loc,
customer cust
WHERE
prod.prod_id = link.prod_id
AND link.region_id = region.region_id
AND region.loc_id = loc.loc_id
AND link.prod_link_deleted = FALSE
AND loc.customer_id = customer_id
AND prod.prod_id = 5874;
This returns the following result:
| prod_id | is_source | prod_link_disabled | location_id | customer_id | customer_name |
| ------------------- | --------- | ------------------ | ----------- | ----------- | --------------- |
| Test Product - 5874 | t | f | 2 | 332 | Test Playground |
| Test Product - 5874 | f | f | 1 | 332 | Test Playground |
This is showing that product 5874 is coming from location 2 (because is_source
is true).
I'd like to compress this information into a single row. I'd like a result that looks like this:
| prod_id | prod_link_disabled | src_location_id | dest_location_id | customer_id | customer_name |
| ------------------- | ------------------ | --------------- | ---------------- | ----------- | --------------- |
| Test Product - 5874 | f | 2 | 1 | 332 | Test Playground |
This compressed the two previous rows into a single row where src_location_id
is 2
(again because that row had is_source
as true) and dest_location_id
is 1
How can I compress the two row result?
It is safe to assume that there are always only two results in the first query.
postgresql pivot row
New contributor
I have a query that returns customer product information that shows which region a product is sourced from.
Query:
SELECT
prod.prod_id,
prod.is_source,
link.prod_link_disabled,
loc.location_id,
loc.customer_id,
cust.customer_name
FROM
product prod,
product_link link,
region region,
location loc,
customer cust
WHERE
prod.prod_id = link.prod_id
AND link.region_id = region.region_id
AND region.loc_id = loc.loc_id
AND link.prod_link_deleted = FALSE
AND loc.customer_id = customer_id
AND prod.prod_id = 5874;
This returns the following result:
| prod_id | is_source | prod_link_disabled | location_id | customer_id | customer_name |
| ------------------- | --------- | ------------------ | ----------- | ----------- | --------------- |
| Test Product - 5874 | t | f | 2 | 332 | Test Playground |
| Test Product - 5874 | f | f | 1 | 332 | Test Playground |
This is showing that product 5874 is coming from location 2 (because is_source
is true).
I'd like to compress this information into a single row. I'd like a result that looks like this:
| prod_id | prod_link_disabled | src_location_id | dest_location_id | customer_id | customer_name |
| ------------------- | ------------------ | --------------- | ---------------- | ----------- | --------------- |
| Test Product - 5874 | f | 2 | 1 | 332 | Test Playground |
This compressed the two previous rows into a single row where src_location_id
is 2
(again because that row had is_source
as true) and dest_location_id
is 1
How can I compress the two row result?
It is safe to assume that there are always only two results in the first query.
postgresql pivot row
postgresql pivot row
New contributor
New contributor
New contributor
asked 11 mins ago
AndyAndy
1013
1013
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
});
}
});
Andy 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%2f230499%2fcan-i-compress-two-rows-with-a-difference-in-two-columns-into-a-single-row-with%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
Andy is a new contributor. Be nice, and check out our Code of Conduct.
Andy is a new contributor. Be nice, and check out our Code of Conduct.
Andy is a new contributor. Be nice, and check out our Code of Conduct.
Andy 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%2f230499%2fcan-i-compress-two-rows-with-a-difference-in-two-columns-into-a-single-row-with%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