Mysql multiple left join on same table for grouping results
up vote
-3
down vote
favorite
Table
This stable stores some history of address changes
Id Name Address Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1
Result I need a result like beleow
Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III
Is it possible to achieve this result with mysql joins
mysql left-join
add a comment |
up vote
-3
down vote
favorite
Table
This stable stores some history of address changes
Id Name Address Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1
Result I need a result like beleow
Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III
Is it possible to achieve this result with mysql joins
mysql left-join
Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
– Raymond Nijland
Nov 19 at 12:36
Will the row of a secondary address be always after the Primary address ?
– Madhur Bhaiya
Nov 20 at 13:09
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
Table
This stable stores some history of address changes
Id Name Address Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1
Result I need a result like beleow
Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III
Is it possible to achieve this result with mysql joins
mysql left-join
Table
This stable stores some history of address changes
Id Name Address Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1
Result I need a result like beleow
Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III
Is it possible to achieve this result with mysql joins
mysql left-join
mysql left-join
edited Nov 19 at 13:50
asked Nov 19 at 12:31
Alex
11
11
Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
– Raymond Nijland
Nov 19 at 12:36
Will the row of a secondary address be always after the Primary address ?
– Madhur Bhaiya
Nov 20 at 13:09
add a comment |
Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
– Raymond Nijland
Nov 19 at 12:36
Will the row of a secondary address be always after the Primary address ?
– Madhur Bhaiya
Nov 20 at 13:09
Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
– Raymond Nijland
Nov 19 at 12:36
Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
– Raymond Nijland
Nov 19 at 12:36
Will the row of a secondary address be always after the Primary address ?
– Madhur Bhaiya
Nov 20 at 13:09
Will the row of a secondary address be always after the Primary address ?
– Madhur Bhaiya
Nov 20 at 13:09
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can try using case when expression
select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename
I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can try using case when expression
select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename
I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46
add a comment |
up vote
0
down vote
You can try using case when expression
select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename
I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46
add a comment |
up vote
0
down vote
up vote
0
down vote
You can try using case when expression
select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename
You can try using case when expression
select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename
answered Nov 19 at 12:37
fa06
9,7231917
9,7231917
I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46
add a comment |
I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46
I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46
I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46
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%2f53374723%2fmysql-multiple-left-join-on-same-table-for-grouping-results%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
Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
– Raymond Nijland
Nov 19 at 12:36
Will the row of a secondary address be always after the Primary address ?
– Madhur Bhaiya
Nov 20 at 13:09