Struggling with Join with and
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to get the account number for a product by joining a table.
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/3
My account table is structured like:
| account_no | line_number | content |
| ---------- | ----------- | ------- |
| CQ01 | 5 | CUST1 |
| CQ01 | 6 | Q |
| CQ88 | 5 | CUST1 |
| CQ88 | 6 | P |
| CQ22 | 5 | CUST2 |
| CQ22 | 6 | P |
My product table is structured like
| warehouse | product | customer | location |
| --------- | ------- | -------- | -------- |
| 55 | ABC DEF | CUST1 | P |
My query I have built is
select p.*, a.account_no from products p
left join accounts a on
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
unfortunately results in a NULL for account_no
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | |
which is not the result I am after.
What am I doing wrong?
Thanks
EDIT:
My expected output would be:
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
As my accounts table only has account_no = 'CQ88' when line_number 5 is 'CUST1' and line_number 6 = 'P'
sql
add a comment |
I am trying to get the account number for a product by joining a table.
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/3
My account table is structured like:
| account_no | line_number | content |
| ---------- | ----------- | ------- |
| CQ01 | 5 | CUST1 |
| CQ01 | 6 | Q |
| CQ88 | 5 | CUST1 |
| CQ88 | 6 | P |
| CQ22 | 5 | CUST2 |
| CQ22 | 6 | P |
My product table is structured like
| warehouse | product | customer | location |
| --------- | ------- | -------- | -------- |
| 55 | ABC DEF | CUST1 | P |
My query I have built is
select p.*, a.account_no from products p
left join accounts a on
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
unfortunately results in a NULL for account_no
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | |
which is not the result I am after.
What am I doing wrong?
Thanks
EDIT:
My expected output would be:
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
As my accounts table only has account_no = 'CQ88' when line_number 5 is 'CUST1' and line_number 6 = 'P'
sql
1
Please share your expected output.
– Mayank Porwal
Nov 16 '18 at 11:28
You need to join accounts twice, once for each line number.
– dnoeth
Nov 16 '18 at 11:28
db-fiddle.com/f/s6B4jcyhMtvGHCvp8FD5A3/0
– dnoeth
Nov 16 '18 at 11:35
@Ian go for my answer
– Ravi
Nov 16 '18 at 11:44
add a comment |
I am trying to get the account number for a product by joining a table.
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/3
My account table is structured like:
| account_no | line_number | content |
| ---------- | ----------- | ------- |
| CQ01 | 5 | CUST1 |
| CQ01 | 6 | Q |
| CQ88 | 5 | CUST1 |
| CQ88 | 6 | P |
| CQ22 | 5 | CUST2 |
| CQ22 | 6 | P |
My product table is structured like
| warehouse | product | customer | location |
| --------- | ------- | -------- | -------- |
| 55 | ABC DEF | CUST1 | P |
My query I have built is
select p.*, a.account_no from products p
left join accounts a on
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
unfortunately results in a NULL for account_no
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | |
which is not the result I am after.
What am I doing wrong?
Thanks
EDIT:
My expected output would be:
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
As my accounts table only has account_no = 'CQ88' when line_number 5 is 'CUST1' and line_number 6 = 'P'
sql
I am trying to get the account number for a product by joining a table.
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/3
My account table is structured like:
| account_no | line_number | content |
| ---------- | ----------- | ------- |
| CQ01 | 5 | CUST1 |
| CQ01 | 6 | Q |
| CQ88 | 5 | CUST1 |
| CQ88 | 6 | P |
| CQ22 | 5 | CUST2 |
| CQ22 | 6 | P |
My product table is structured like
| warehouse | product | customer | location |
| --------- | ------- | -------- | -------- |
| 55 | ABC DEF | CUST1 | P |
My query I have built is
select p.*, a.account_no from products p
left join accounts a on
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
unfortunately results in a NULL for account_no
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | |
which is not the result I am after.
What am I doing wrong?
Thanks
EDIT:
My expected output would be:
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
As my accounts table only has account_no = 'CQ88' when line_number 5 is 'CUST1' and line_number 6 = 'P'
sql
sql
edited Nov 16 '18 at 11:40
Ian
asked Nov 16 '18 at 11:26
IanIan
556
556
1
Please share your expected output.
– Mayank Porwal
Nov 16 '18 at 11:28
You need to join accounts twice, once for each line number.
– dnoeth
Nov 16 '18 at 11:28
db-fiddle.com/f/s6B4jcyhMtvGHCvp8FD5A3/0
– dnoeth
Nov 16 '18 at 11:35
@Ian go for my answer
– Ravi
Nov 16 '18 at 11:44
add a comment |
1
Please share your expected output.
– Mayank Porwal
Nov 16 '18 at 11:28
You need to join accounts twice, once for each line number.
– dnoeth
Nov 16 '18 at 11:28
db-fiddle.com/f/s6B4jcyhMtvGHCvp8FD5A3/0
– dnoeth
Nov 16 '18 at 11:35
@Ian go for my answer
– Ravi
Nov 16 '18 at 11:44
1
1
Please share your expected output.
– Mayank Porwal
Nov 16 '18 at 11:28
Please share your expected output.
– Mayank Porwal
Nov 16 '18 at 11:28
You need to join accounts twice, once for each line number.
– dnoeth
Nov 16 '18 at 11:28
You need to join accounts twice, once for each line number.
– dnoeth
Nov 16 '18 at 11:28
db-fiddle.com/f/s6B4jcyhMtvGHCvp8FD5A3/0
– dnoeth
Nov 16 '18 at 11:35
db-fiddle.com/f/s6B4jcyhMtvGHCvp8FD5A3/0
– dnoeth
Nov 16 '18 at 11:35
@Ian go for my answer
– Ravi
Nov 16 '18 at 11:44
@Ian go for my answer
– Ravi
Nov 16 '18 at 11:44
add a comment |
4 Answers
4
active
oldest
votes
The problem is with your ON conditions:
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
Since you have used an AND operator, so both conditions need to be True for a match to be found. And there's no single row in Accounts table having line number both 5 and 6.
So, change and to or. It should solve your issue.
Let me know if it works.
add a comment |
I am guessing you want two joins:
select p.*,
a5.account_no as customer_account_no,
a6.account_no as location_account_no
from products p left join
accounts a5
on a5.content = p.customer and
a5.line_number = 5 left join
accounts a6
on a6.content = p.location and
a6.line_number = 6;
add a comment |
I solved it.
I needed to re-structure my accounts table so there were one row per account_no
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/6
I then performed a simple join
select p.*, new_accounts.account_no from products p
left join (
select distinct a.account_no, concat(b.content , '-',c.content) as 'custloc' from accounts a
left join (select * from accounts where line_number = 5) b on a.account_no = b.account_no
left join (select * from accounts where line_number = 6) c on a.account_no = c.account_no
) new_accounts ON concat(p.customer,'-',p.location) = new_accounts.custloc;
resulted in
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
add a comment |
select p.*, a.account_no from products p
inner join accounts a on
(a.content = p.customer and a.line_number = 5)
inner join accounts b on
(b.content = p.location and b.line_number = 6) and a.account_no = b.account_no
Thanks, sadly this picks up two rows. It results with an account_no "CQ01" which is not what I need. In my accounts table, CQ01 has a reference to CUST1 and location "Q", but my product is looking for CUST1 and location "P"
– Ian
Nov 16 '18 at 11:50
Not possible to return two rows
– Ravi
Nov 16 '18 at 11:57
What does that do? How does it answer the question? Don't just blurt out code. Explain yourself ! stackoverflow.com/help/how-to-answer
– Rob
Nov 16 '18 at 12:13
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%2f53336949%2fstruggling-with-join-with-and%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is with your ON conditions:
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
Since you have used an AND operator, so both conditions need to be True for a match to be found. And there's no single row in Accounts table having line number both 5 and 6.
So, change and to or. It should solve your issue.
Let me know if it works.
add a comment |
The problem is with your ON conditions:
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
Since you have used an AND operator, so both conditions need to be True for a match to be found. And there's no single row in Accounts table having line number both 5 and 6.
So, change and to or. It should solve your issue.
Let me know if it works.
add a comment |
The problem is with your ON conditions:
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
Since you have used an AND operator, so both conditions need to be True for a match to be found. And there's no single row in Accounts table having line number both 5 and 6.
So, change and to or. It should solve your issue.
Let me know if it works.
The problem is with your ON conditions:
(a.content = p.customer and a.line_number = 5)
and
(a.content = p.location and a.line_number = 6);
Since you have used an AND operator, so both conditions need to be True for a match to be found. And there's no single row in Accounts table having line number both 5 and 6.
So, change and to or. It should solve your issue.
Let me know if it works.
answered Nov 16 '18 at 11:32
Mayank PorwalMayank Porwal
5,0182725
5,0182725
add a comment |
add a comment |
I am guessing you want two joins:
select p.*,
a5.account_no as customer_account_no,
a6.account_no as location_account_no
from products p left join
accounts a5
on a5.content = p.customer and
a5.line_number = 5 left join
accounts a6
on a6.content = p.location and
a6.line_number = 6;
add a comment |
I am guessing you want two joins:
select p.*,
a5.account_no as customer_account_no,
a6.account_no as location_account_no
from products p left join
accounts a5
on a5.content = p.customer and
a5.line_number = 5 left join
accounts a6
on a6.content = p.location and
a6.line_number = 6;
add a comment |
I am guessing you want two joins:
select p.*,
a5.account_no as customer_account_no,
a6.account_no as location_account_no
from products p left join
accounts a5
on a5.content = p.customer and
a5.line_number = 5 left join
accounts a6
on a6.content = p.location and
a6.line_number = 6;
I am guessing you want two joins:
select p.*,
a5.account_no as customer_account_no,
a6.account_no as location_account_no
from products p left join
accounts a5
on a5.content = p.customer and
a5.line_number = 5 left join
accounts a6
on a6.content = p.location and
a6.line_number = 6;
answered Nov 16 '18 at 11:34
Gordon LinoffGordon Linoff
794k37318421
794k37318421
add a comment |
add a comment |
I solved it.
I needed to re-structure my accounts table so there were one row per account_no
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/6
I then performed a simple join
select p.*, new_accounts.account_no from products p
left join (
select distinct a.account_no, concat(b.content , '-',c.content) as 'custloc' from accounts a
left join (select * from accounts where line_number = 5) b on a.account_no = b.account_no
left join (select * from accounts where line_number = 6) c on a.account_no = c.account_no
) new_accounts ON concat(p.customer,'-',p.location) = new_accounts.custloc;
resulted in
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
add a comment |
I solved it.
I needed to re-structure my accounts table so there were one row per account_no
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/6
I then performed a simple join
select p.*, new_accounts.account_no from products p
left join (
select distinct a.account_no, concat(b.content , '-',c.content) as 'custloc' from accounts a
left join (select * from accounts where line_number = 5) b on a.account_no = b.account_no
left join (select * from accounts where line_number = 6) c on a.account_no = c.account_no
) new_accounts ON concat(p.customer,'-',p.location) = new_accounts.custloc;
resulted in
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
add a comment |
I solved it.
I needed to re-structure my accounts table so there were one row per account_no
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/6
I then performed a simple join
select p.*, new_accounts.account_no from products p
left join (
select distinct a.account_no, concat(b.content , '-',c.content) as 'custloc' from accounts a
left join (select * from accounts where line_number = 5) b on a.account_no = b.account_no
left join (select * from accounts where line_number = 6) c on a.account_no = c.account_no
) new_accounts ON concat(p.customer,'-',p.location) = new_accounts.custloc;
resulted in
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
I solved it.
I needed to re-structure my accounts table so there were one row per account_no
https://www.db-fiddle.com/f/q6GJFqFqrNrDgMd3fnmEwG/6
I then performed a simple join
select p.*, new_accounts.account_no from products p
left join (
select distinct a.account_no, concat(b.content , '-',c.content) as 'custloc' from accounts a
left join (select * from accounts where line_number = 5) b on a.account_no = b.account_no
left join (select * from accounts where line_number = 6) c on a.account_no = c.account_no
) new_accounts ON concat(p.customer,'-',p.location) = new_accounts.custloc;
resulted in
| warehouse | product | customer | location | account_no |
| --------- | ------- | -------- | -------- | ---------- |
| 55 | ABC DEF | CUST1 | P | CQ88 |
answered Nov 16 '18 at 12:12
IanIan
556
556
add a comment |
add a comment |
select p.*, a.account_no from products p
inner join accounts a on
(a.content = p.customer and a.line_number = 5)
inner join accounts b on
(b.content = p.location and b.line_number = 6) and a.account_no = b.account_no
Thanks, sadly this picks up two rows. It results with an account_no "CQ01" which is not what I need. In my accounts table, CQ01 has a reference to CUST1 and location "Q", but my product is looking for CUST1 and location "P"
– Ian
Nov 16 '18 at 11:50
Not possible to return two rows
– Ravi
Nov 16 '18 at 11:57
What does that do? How does it answer the question? Don't just blurt out code. Explain yourself ! stackoverflow.com/help/how-to-answer
– Rob
Nov 16 '18 at 12:13
add a comment |
select p.*, a.account_no from products p
inner join accounts a on
(a.content = p.customer and a.line_number = 5)
inner join accounts b on
(b.content = p.location and b.line_number = 6) and a.account_no = b.account_no
Thanks, sadly this picks up two rows. It results with an account_no "CQ01" which is not what I need. In my accounts table, CQ01 has a reference to CUST1 and location "Q", but my product is looking for CUST1 and location "P"
– Ian
Nov 16 '18 at 11:50
Not possible to return two rows
– Ravi
Nov 16 '18 at 11:57
What does that do? How does it answer the question? Don't just blurt out code. Explain yourself ! stackoverflow.com/help/how-to-answer
– Rob
Nov 16 '18 at 12:13
add a comment |
select p.*, a.account_no from products p
inner join accounts a on
(a.content = p.customer and a.line_number = 5)
inner join accounts b on
(b.content = p.location and b.line_number = 6) and a.account_no = b.account_no
select p.*, a.account_no from products p
inner join accounts a on
(a.content = p.customer and a.line_number = 5)
inner join accounts b on
(b.content = p.location and b.line_number = 6) and a.account_no = b.account_no
answered Nov 16 '18 at 11:42
RaviRavi
9821315
9821315
Thanks, sadly this picks up two rows. It results with an account_no "CQ01" which is not what I need. In my accounts table, CQ01 has a reference to CUST1 and location "Q", but my product is looking for CUST1 and location "P"
– Ian
Nov 16 '18 at 11:50
Not possible to return two rows
– Ravi
Nov 16 '18 at 11:57
What does that do? How does it answer the question? Don't just blurt out code. Explain yourself ! stackoverflow.com/help/how-to-answer
– Rob
Nov 16 '18 at 12:13
add a comment |
Thanks, sadly this picks up two rows. It results with an account_no "CQ01" which is not what I need. In my accounts table, CQ01 has a reference to CUST1 and location "Q", but my product is looking for CUST1 and location "P"
– Ian
Nov 16 '18 at 11:50
Not possible to return two rows
– Ravi
Nov 16 '18 at 11:57
What does that do? How does it answer the question? Don't just blurt out code. Explain yourself ! stackoverflow.com/help/how-to-answer
– Rob
Nov 16 '18 at 12:13
Thanks, sadly this picks up two rows. It results with an account_no "CQ01" which is not what I need. In my accounts table, CQ01 has a reference to CUST1 and location "Q", but my product is looking for CUST1 and location "P"
– Ian
Nov 16 '18 at 11:50
Thanks, sadly this picks up two rows. It results with an account_no "CQ01" which is not what I need. In my accounts table, CQ01 has a reference to CUST1 and location "Q", but my product is looking for CUST1 and location "P"
– Ian
Nov 16 '18 at 11:50
Not possible to return two rows
– Ravi
Nov 16 '18 at 11:57
Not possible to return two rows
– Ravi
Nov 16 '18 at 11:57
What does that do? How does it answer the question? Don't just blurt out code. Explain yourself ! stackoverflow.com/help/how-to-answer
– Rob
Nov 16 '18 at 12:13
What does that do? How does it answer the question? Don't just blurt out code. Explain yourself ! stackoverflow.com/help/how-to-answer
– Rob
Nov 16 '18 at 12:13
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%2f53336949%2fstruggling-with-join-with-and%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
1
Please share your expected output.
– Mayank Porwal
Nov 16 '18 at 11:28
You need to join accounts twice, once for each line number.
– dnoeth
Nov 16 '18 at 11:28
db-fiddle.com/f/s6B4jcyhMtvGHCvp8FD5A3/0
– dnoeth
Nov 16 '18 at 11:35
@Ian go for my answer
– Ravi
Nov 16 '18 at 11:44