Add a new column to SELECT if elements of an INNER JOIN exists
I have 3 table PERSONS, COMPANIES and PERSON_CUSTOMER_COMPANY which makes the relation n-to-n if the person is a customer of the company (a person may be others relations with each company).
This query returns all companies that have a relation with a given person as a Customer (line 3 inner join).
select co.name from COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
INNER JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
I need to change this query to return all companies from a person even if they are not related in the PERSON_CUSTOMER_COMPANY and an extra field indicating if the person is a customer of the company.
Something like "isCustomer"
select co.name, isCustomer from COMPANIES co ...
sql hibernate
add a comment |
I have 3 table PERSONS, COMPANIES and PERSON_CUSTOMER_COMPANY which makes the relation n-to-n if the person is a customer of the company (a person may be others relations with each company).
This query returns all companies that have a relation with a given person as a Customer (line 3 inner join).
select co.name from COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
INNER JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
I need to change this query to return all companies from a person even if they are not related in the PERSON_CUSTOMER_COMPANY and an extra field indicating if the person is a customer of the company.
Something like "isCustomer"
select co.name, isCustomer from COMPANIES co ...
sql hibernate
I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
– Gordon Linoff
Nov 19 '18 at 18:59
@GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:05
add a comment |
I have 3 table PERSONS, COMPANIES and PERSON_CUSTOMER_COMPANY which makes the relation n-to-n if the person is a customer of the company (a person may be others relations with each company).
This query returns all companies that have a relation with a given person as a Customer (line 3 inner join).
select co.name from COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
INNER JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
I need to change this query to return all companies from a person even if they are not related in the PERSON_CUSTOMER_COMPANY and an extra field indicating if the person is a customer of the company.
Something like "isCustomer"
select co.name, isCustomer from COMPANIES co ...
sql hibernate
I have 3 table PERSONS, COMPANIES and PERSON_CUSTOMER_COMPANY which makes the relation n-to-n if the person is a customer of the company (a person may be others relations with each company).
This query returns all companies that have a relation with a given person as a Customer (line 3 inner join).
select co.name from COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
INNER JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
I need to change this query to return all companies from a person even if they are not related in the PERSON_CUSTOMER_COMPANY and an extra field indicating if the person is a customer of the company.
Something like "isCustomer"
select co.name, isCustomer from COMPANIES co ...
sql hibernate
sql hibernate
asked Nov 19 '18 at 18:53
Thadeu Antonio Ferreira MeloThadeu Antonio Ferreira Melo
194113
194113
I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
– Gordon Linoff
Nov 19 '18 at 18:59
@GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:05
add a comment |
I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
– Gordon Linoff
Nov 19 '18 at 18:59
@GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:05
I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
– Gordon Linoff
Nov 19 '18 at 18:59
I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
– Gordon Linoff
Nov 19 '18 at 18:59
@GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:05
@GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:05
add a comment |
2 Answers
2
active
oldest
votes
An inner join
will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company
table, then you need an outer join
instead. Then you can use a case
statement to create the new column:
SELECT co.name,
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02
add a comment |
I would probably use exists
:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
This only uses PERSON
, because that is the JOIN
you use in your query.
I suspect you really want:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
In either case, only two table are necessary.
This returns all companies, not just those where the person has a relationship... OP statesreturn all companies from a person
- I think this is why thejoin
is needed.
– sgeddes
Nov 19 '18 at 19:03
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%2f53380959%2fadd-a-new-column-to-select-if-elements-of-an-inner-join-exists%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
An inner join
will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company
table, then you need an outer join
instead. Then you can use a case
statement to create the new column:
SELECT co.name,
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02
add a comment |
An inner join
will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company
table, then you need an outer join
instead. Then you can use a case
statement to create the new column:
SELECT co.name,
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02
add a comment |
An inner join
will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company
table, then you need an outer join
instead. Then you can use a case
statement to create the new column:
SELECT co.name,
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
An inner join
will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company
table, then you need an outer join
instead. Then you can use a case
statement to create the new column:
SELECT co.name,
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;
answered Nov 19 '18 at 18:57
sgeddessgeddes
56.5k53963
56.5k53963
Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02
add a comment |
Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02
Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02
Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02
add a comment |
I would probably use exists
:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
This only uses PERSON
, because that is the JOIN
you use in your query.
I suspect you really want:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
In either case, only two table are necessary.
This returns all companies, not just those where the person has a relationship... OP statesreturn all companies from a person
- I think this is why thejoin
is needed.
– sgeddes
Nov 19 '18 at 19:03
add a comment |
I would probably use exists
:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
This only uses PERSON
, because that is the JOIN
you use in your query.
I suspect you really want:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
In either case, only two table are necessary.
This returns all companies, not just those where the person has a relationship... OP statesreturn all companies from a person
- I think this is why thejoin
is needed.
– sgeddes
Nov 19 '18 at 19:03
add a comment |
I would probably use exists
:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
This only uses PERSON
, because that is the JOIN
you use in your query.
I suspect you really want:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
In either case, only two table are necessary.
I would probably use exists
:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
This only uses PERSON
, because that is the JOIN
you use in your query.
I suspect you really want:
SELECT co.name,
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;
In either case, only two table are necessary.
answered Nov 19 '18 at 19:00
Gordon LinoffGordon Linoff
761k35294399
761k35294399
This returns all companies, not just those where the person has a relationship... OP statesreturn all companies from a person
- I think this is why thejoin
is needed.
– sgeddes
Nov 19 '18 at 19:03
add a comment |
This returns all companies, not just those where the person has a relationship... OP statesreturn all companies from a person
- I think this is why thejoin
is needed.
– sgeddes
Nov 19 '18 at 19:03
This returns all companies, not just those where the person has a relationship... OP states
return all companies from a person
- I think this is why the join
is needed.– sgeddes
Nov 19 '18 at 19:03
This returns all companies, not just those where the person has a relationship... OP states
return all companies from a person
- I think this is why the join
is needed.– sgeddes
Nov 19 '18 at 19:03
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53380959%2fadd-a-new-column-to-select-if-elements-of-an-inner-join-exists%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
I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
– Gordon Linoff
Nov 19 '18 at 18:59
@GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:05