mysql how to query based on latest relationship data
I have two table
- companies
- company_relocation_histories
Relationship is Company HAS MANY Relocation History
Sample data table companies
Sample data table company_relocation_histories
Sample DB -> https://www.dropbox.com/s/e8uvuc9vvgacz0q/test.sql?dl=0
I want to query all Company that has recently relocate to certain place, for example to FR. Only last location will be counted for.
Expected data is ONLY company id 1 (MIB) will be listed.
Company id 3 (SKD) will be excluded because although they have relocate to FR before, the last relocation is SG.
Here is my take on the SQL query which is not working yet (wrong result). How to solve this? Thanks Stack Overflow!
SELECT *
FROM `companies`
WHERE EXISTS (SELECT *
FROM `company_relocation_histories`
WHERE `companies`.`id` =
`company_relocation_histories`.`company_id`
AND `relocation_location` = 'FR'
AND `id` = (SELECT Max(id)
FROM `company_relocation_histories` AS `sub`
WHERE sub.relocation_location =
company_relocation_histories.relocation_location))
mysql
add a comment |
I have two table
- companies
- company_relocation_histories
Relationship is Company HAS MANY Relocation History
Sample data table companies
Sample data table company_relocation_histories
Sample DB -> https://www.dropbox.com/s/e8uvuc9vvgacz0q/test.sql?dl=0
I want to query all Company that has recently relocate to certain place, for example to FR. Only last location will be counted for.
Expected data is ONLY company id 1 (MIB) will be listed.
Company id 3 (SKD) will be excluded because although they have relocate to FR before, the last relocation is SG.
Here is my take on the SQL query which is not working yet (wrong result). How to solve this? Thanks Stack Overflow!
SELECT *
FROM `companies`
WHERE EXISTS (SELECT *
FROM `company_relocation_histories`
WHERE `companies`.`id` =
`company_relocation_histories`.`company_id`
AND `relocation_location` = 'FR'
AND `id` = (SELECT Max(id)
FROM `company_relocation_histories` AS `sub`
WHERE sub.relocation_location =
company_relocation_histories.relocation_location))
mysql
add a comment |
I have two table
- companies
- company_relocation_histories
Relationship is Company HAS MANY Relocation History
Sample data table companies
Sample data table company_relocation_histories
Sample DB -> https://www.dropbox.com/s/e8uvuc9vvgacz0q/test.sql?dl=0
I want to query all Company that has recently relocate to certain place, for example to FR. Only last location will be counted for.
Expected data is ONLY company id 1 (MIB) will be listed.
Company id 3 (SKD) will be excluded because although they have relocate to FR before, the last relocation is SG.
Here is my take on the SQL query which is not working yet (wrong result). How to solve this? Thanks Stack Overflow!
SELECT *
FROM `companies`
WHERE EXISTS (SELECT *
FROM `company_relocation_histories`
WHERE `companies`.`id` =
`company_relocation_histories`.`company_id`
AND `relocation_location` = 'FR'
AND `id` = (SELECT Max(id)
FROM `company_relocation_histories` AS `sub`
WHERE sub.relocation_location =
company_relocation_histories.relocation_location))
mysql
I have two table
- companies
- company_relocation_histories
Relationship is Company HAS MANY Relocation History
Sample data table companies
Sample data table company_relocation_histories
Sample DB -> https://www.dropbox.com/s/e8uvuc9vvgacz0q/test.sql?dl=0
I want to query all Company that has recently relocate to certain place, for example to FR. Only last location will be counted for.
Expected data is ONLY company id 1 (MIB) will be listed.
Company id 3 (SKD) will be excluded because although they have relocate to FR before, the last relocation is SG.
Here is my take on the SQL query which is not working yet (wrong result). How to solve this? Thanks Stack Overflow!
SELECT *
FROM `companies`
WHERE EXISTS (SELECT *
FROM `company_relocation_histories`
WHERE `companies`.`id` =
`company_relocation_histories`.`company_id`
AND `relocation_location` = 'FR'
AND `id` = (SELECT Max(id)
FROM `company_relocation_histories` AS `sub`
WHERE sub.relocation_location =
company_relocation_histories.relocation_location))
mysql
mysql
edited Jan 2 at 6:56


Derviş Kayımbaşıoğlu
15.5k22042
15.5k22042
asked Jan 2 at 6:47
cyberflycyberfly
2,67273855
2,67273855
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
YOu could use a subquery for max(id) and count > 1
SELECT *
FROM `companies` c
INNER JOIN company_relocation_histories h on c.id = h.company_id
INNER JOIN
(
SELECT company_id, Max(id) max_id
FROM `company_relocation_histories` AS `sub`
group by company_id
having count(*) >1
) t on t.company_id = c.id
and t.max_id = h.id
and h.relocation_location ='FR'
In this way you have all the company with more than one location and related to the last location
Thanks, sadly I have sql syntax error when tried to run this query, not sure where is the problem
– cyberfly
Jan 2 at 7:55
you should show me the exact error message .. any way i hav update the answer because someone changed wrongly ..
– scaisEdge
Jan 2 at 8:00
hi just tested the updated answer, here is the error check the right syntax near 'on t.company_id = c.id and t.max_id = h.id'
– cyberfly
Jan 2 at 8:03
answer update .. wrong ((
– scaisEdge
Jan 2 at 8:08
Thanks query working now. However the is problem 1. Relocation is not filtered by specific location, for example FR 2. It will exclude company that only relocate one time I modified abit but still no yet solved the problem ``` SELECT * FROMcompanies
c INNER JOIN company_relocation_histories h on c.id = h.company_id INNER JOIN ( SELECT company_id, Max(id) max_id FROMcompany_relocation_histories
ASsub
where relocation_location='FR' group by company_id having count(*) >0 ) t on t.company_id = c.id and t.max_id = h.id ```
– cyberfly
Jan 2 at 8:20
|
show 3 more comments
Check this query. inner query groups reloaction_histories to get the company_id's which has minimum two or more relocations.
select * from companies
where id in (
select company_id
from company_relocation_histories
group by company_id
having count(*) = 2
);
Edit: According to OP's comment
SELECT *
FROM companies
WHERE id IN (SELECT company_id
FROM (SELECT company_id,
Lead(relocation_location) OVER(ORDER BY id) x,
Count(*) OVER (partition BY company_id) cnt
FROM company_relocation_histories) t
WHERE x = 'FR'
AND cnt = 2
GROUP BY company_id);
Hi thanks for the query. Actually I also want company that has one relocation, as long as the last relocation is specific place FR. Your query also include company id 3, even though the last relocation for company id 3 is SG and not FR
– cyberfly
Jan 2 at 7:48
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%2f54002312%2fmysql-how-to-query-based-on-latest-relationship-data%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
YOu could use a subquery for max(id) and count > 1
SELECT *
FROM `companies` c
INNER JOIN company_relocation_histories h on c.id = h.company_id
INNER JOIN
(
SELECT company_id, Max(id) max_id
FROM `company_relocation_histories` AS `sub`
group by company_id
having count(*) >1
) t on t.company_id = c.id
and t.max_id = h.id
and h.relocation_location ='FR'
In this way you have all the company with more than one location and related to the last location
Thanks, sadly I have sql syntax error when tried to run this query, not sure where is the problem
– cyberfly
Jan 2 at 7:55
you should show me the exact error message .. any way i hav update the answer because someone changed wrongly ..
– scaisEdge
Jan 2 at 8:00
hi just tested the updated answer, here is the error check the right syntax near 'on t.company_id = c.id and t.max_id = h.id'
– cyberfly
Jan 2 at 8:03
answer update .. wrong ((
– scaisEdge
Jan 2 at 8:08
Thanks query working now. However the is problem 1. Relocation is not filtered by specific location, for example FR 2. It will exclude company that only relocate one time I modified abit but still no yet solved the problem ``` SELECT * FROMcompanies
c INNER JOIN company_relocation_histories h on c.id = h.company_id INNER JOIN ( SELECT company_id, Max(id) max_id FROMcompany_relocation_histories
ASsub
where relocation_location='FR' group by company_id having count(*) >0 ) t on t.company_id = c.id and t.max_id = h.id ```
– cyberfly
Jan 2 at 8:20
|
show 3 more comments
YOu could use a subquery for max(id) and count > 1
SELECT *
FROM `companies` c
INNER JOIN company_relocation_histories h on c.id = h.company_id
INNER JOIN
(
SELECT company_id, Max(id) max_id
FROM `company_relocation_histories` AS `sub`
group by company_id
having count(*) >1
) t on t.company_id = c.id
and t.max_id = h.id
and h.relocation_location ='FR'
In this way you have all the company with more than one location and related to the last location
Thanks, sadly I have sql syntax error when tried to run this query, not sure where is the problem
– cyberfly
Jan 2 at 7:55
you should show me the exact error message .. any way i hav update the answer because someone changed wrongly ..
– scaisEdge
Jan 2 at 8:00
hi just tested the updated answer, here is the error check the right syntax near 'on t.company_id = c.id and t.max_id = h.id'
– cyberfly
Jan 2 at 8:03
answer update .. wrong ((
– scaisEdge
Jan 2 at 8:08
Thanks query working now. However the is problem 1. Relocation is not filtered by specific location, for example FR 2. It will exclude company that only relocate one time I modified abit but still no yet solved the problem ``` SELECT * FROMcompanies
c INNER JOIN company_relocation_histories h on c.id = h.company_id INNER JOIN ( SELECT company_id, Max(id) max_id FROMcompany_relocation_histories
ASsub
where relocation_location='FR' group by company_id having count(*) >0 ) t on t.company_id = c.id and t.max_id = h.id ```
– cyberfly
Jan 2 at 8:20
|
show 3 more comments
YOu could use a subquery for max(id) and count > 1
SELECT *
FROM `companies` c
INNER JOIN company_relocation_histories h on c.id = h.company_id
INNER JOIN
(
SELECT company_id, Max(id) max_id
FROM `company_relocation_histories` AS `sub`
group by company_id
having count(*) >1
) t on t.company_id = c.id
and t.max_id = h.id
and h.relocation_location ='FR'
In this way you have all the company with more than one location and related to the last location
YOu could use a subquery for max(id) and count > 1
SELECT *
FROM `companies` c
INNER JOIN company_relocation_histories h on c.id = h.company_id
INNER JOIN
(
SELECT company_id, Max(id) max_id
FROM `company_relocation_histories` AS `sub`
group by company_id
having count(*) >1
) t on t.company_id = c.id
and t.max_id = h.id
and h.relocation_location ='FR'
In this way you have all the company with more than one location and related to the last location
edited Jan 2 at 8:47
answered Jan 2 at 7:06
scaisEdgescaisEdge
96.1k105272
96.1k105272
Thanks, sadly I have sql syntax error when tried to run this query, not sure where is the problem
– cyberfly
Jan 2 at 7:55
you should show me the exact error message .. any way i hav update the answer because someone changed wrongly ..
– scaisEdge
Jan 2 at 8:00
hi just tested the updated answer, here is the error check the right syntax near 'on t.company_id = c.id and t.max_id = h.id'
– cyberfly
Jan 2 at 8:03
answer update .. wrong ((
– scaisEdge
Jan 2 at 8:08
Thanks query working now. However the is problem 1. Relocation is not filtered by specific location, for example FR 2. It will exclude company that only relocate one time I modified abit but still no yet solved the problem ``` SELECT * FROMcompanies
c INNER JOIN company_relocation_histories h on c.id = h.company_id INNER JOIN ( SELECT company_id, Max(id) max_id FROMcompany_relocation_histories
ASsub
where relocation_location='FR' group by company_id having count(*) >0 ) t on t.company_id = c.id and t.max_id = h.id ```
– cyberfly
Jan 2 at 8:20
|
show 3 more comments
Thanks, sadly I have sql syntax error when tried to run this query, not sure where is the problem
– cyberfly
Jan 2 at 7:55
you should show me the exact error message .. any way i hav update the answer because someone changed wrongly ..
– scaisEdge
Jan 2 at 8:00
hi just tested the updated answer, here is the error check the right syntax near 'on t.company_id = c.id and t.max_id = h.id'
– cyberfly
Jan 2 at 8:03
answer update .. wrong ((
– scaisEdge
Jan 2 at 8:08
Thanks query working now. However the is problem 1. Relocation is not filtered by specific location, for example FR 2. It will exclude company that only relocate one time I modified abit but still no yet solved the problem ``` SELECT * FROMcompanies
c INNER JOIN company_relocation_histories h on c.id = h.company_id INNER JOIN ( SELECT company_id, Max(id) max_id FROMcompany_relocation_histories
ASsub
where relocation_location='FR' group by company_id having count(*) >0 ) t on t.company_id = c.id and t.max_id = h.id ```
– cyberfly
Jan 2 at 8:20
Thanks, sadly I have sql syntax error when tried to run this query, not sure where is the problem
– cyberfly
Jan 2 at 7:55
Thanks, sadly I have sql syntax error when tried to run this query, not sure where is the problem
– cyberfly
Jan 2 at 7:55
you should show me the exact error message .. any way i hav update the answer because someone changed wrongly ..
– scaisEdge
Jan 2 at 8:00
you should show me the exact error message .. any way i hav update the answer because someone changed wrongly ..
– scaisEdge
Jan 2 at 8:00
hi just tested the updated answer, here is the error check the right syntax near 'on t.company_id = c.id and t.max_id = h.id'
– cyberfly
Jan 2 at 8:03
hi just tested the updated answer, here is the error check the right syntax near 'on t.company_id = c.id and t.max_id = h.id'
– cyberfly
Jan 2 at 8:03
answer update .. wrong ((
– scaisEdge
Jan 2 at 8:08
answer update .. wrong ((
– scaisEdge
Jan 2 at 8:08
Thanks query working now. However the is problem 1. Relocation is not filtered by specific location, for example FR 2. It will exclude company that only relocate one time I modified abit but still no yet solved the problem ``` SELECT * FROM
companies
c INNER JOIN company_relocation_histories h on c.id = h.company_id INNER JOIN ( SELECT company_id, Max(id) max_id FROM company_relocation_histories
AS sub
where relocation_location='FR' group by company_id having count(*) >0 ) t on t.company_id = c.id and t.max_id = h.id ```– cyberfly
Jan 2 at 8:20
Thanks query working now. However the is problem 1. Relocation is not filtered by specific location, for example FR 2. It will exclude company that only relocate one time I modified abit but still no yet solved the problem ``` SELECT * FROM
companies
c INNER JOIN company_relocation_histories h on c.id = h.company_id INNER JOIN ( SELECT company_id, Max(id) max_id FROM company_relocation_histories
AS sub
where relocation_location='FR' group by company_id having count(*) >0 ) t on t.company_id = c.id and t.max_id = h.id ```– cyberfly
Jan 2 at 8:20
|
show 3 more comments
Check this query. inner query groups reloaction_histories to get the company_id's which has minimum two or more relocations.
select * from companies
where id in (
select company_id
from company_relocation_histories
group by company_id
having count(*) = 2
);
Edit: According to OP's comment
SELECT *
FROM companies
WHERE id IN (SELECT company_id
FROM (SELECT company_id,
Lead(relocation_location) OVER(ORDER BY id) x,
Count(*) OVER (partition BY company_id) cnt
FROM company_relocation_histories) t
WHERE x = 'FR'
AND cnt = 2
GROUP BY company_id);
Hi thanks for the query. Actually I also want company that has one relocation, as long as the last relocation is specific place FR. Your query also include company id 3, even though the last relocation for company id 3 is SG and not FR
– cyberfly
Jan 2 at 7:48
add a comment |
Check this query. inner query groups reloaction_histories to get the company_id's which has minimum two or more relocations.
select * from companies
where id in (
select company_id
from company_relocation_histories
group by company_id
having count(*) = 2
);
Edit: According to OP's comment
SELECT *
FROM companies
WHERE id IN (SELECT company_id
FROM (SELECT company_id,
Lead(relocation_location) OVER(ORDER BY id) x,
Count(*) OVER (partition BY company_id) cnt
FROM company_relocation_histories) t
WHERE x = 'FR'
AND cnt = 2
GROUP BY company_id);
Hi thanks for the query. Actually I also want company that has one relocation, as long as the last relocation is specific place FR. Your query also include company id 3, even though the last relocation for company id 3 is SG and not FR
– cyberfly
Jan 2 at 7:48
add a comment |
Check this query. inner query groups reloaction_histories to get the company_id's which has minimum two or more relocations.
select * from companies
where id in (
select company_id
from company_relocation_histories
group by company_id
having count(*) = 2
);
Edit: According to OP's comment
SELECT *
FROM companies
WHERE id IN (SELECT company_id
FROM (SELECT company_id,
Lead(relocation_location) OVER(ORDER BY id) x,
Count(*) OVER (partition BY company_id) cnt
FROM company_relocation_histories) t
WHERE x = 'FR'
AND cnt = 2
GROUP BY company_id);
Check this query. inner query groups reloaction_histories to get the company_id's which has minimum two or more relocations.
select * from companies
where id in (
select company_id
from company_relocation_histories
group by company_id
having count(*) = 2
);
Edit: According to OP's comment
SELECT *
FROM companies
WHERE id IN (SELECT company_id
FROM (SELECT company_id,
Lead(relocation_location) OVER(ORDER BY id) x,
Count(*) OVER (partition BY company_id) cnt
FROM company_relocation_histories) t
WHERE x = 'FR'
AND cnt = 2
GROUP BY company_id);
edited Jan 2 at 8:07
answered Jan 2 at 7:03


Derviş KayımbaşıoğluDerviş Kayımbaşıoğlu
15.5k22042
15.5k22042
Hi thanks for the query. Actually I also want company that has one relocation, as long as the last relocation is specific place FR. Your query also include company id 3, even though the last relocation for company id 3 is SG and not FR
– cyberfly
Jan 2 at 7:48
add a comment |
Hi thanks for the query. Actually I also want company that has one relocation, as long as the last relocation is specific place FR. Your query also include company id 3, even though the last relocation for company id 3 is SG and not FR
– cyberfly
Jan 2 at 7:48
Hi thanks for the query. Actually I also want company that has one relocation, as long as the last relocation is specific place FR. Your query also include company id 3, even though the last relocation for company id 3 is SG and not FR
– cyberfly
Jan 2 at 7:48
Hi thanks for the query. Actually I also want company that has one relocation, as long as the last relocation is specific place FR. Your query also include company id 3, even though the last relocation for company id 3 is SG and not FR
– cyberfly
Jan 2 at 7:48
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%2f54002312%2fmysql-how-to-query-based-on-latest-relationship-data%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