Query that selects IDs of pair of subsidiaries that have exactly the same movies










-2















So, I have an intermediate table in MySQL called "Inventory", with two PKs: idMovie and idSubsidiary. That table looks like this:



----------------------------------
idMovie (int) | idSubsidiary (int)
----------------------------------
0 | 0
2 | 0
1 | 1
3 | 2
----------------------------------


I want to select the IDs of the pair of subsidiaries that have exactly the same movies.



For that, I was thinking about something like this:



select distinct inv1.idSubsidiary, inv2.idSubsidiary
from inventory inv1
join inventory inv2
on inv1.idSubsidiary <> inv2.idSubsidiary
where not exists (

SELECT i1.idSubsidiary, i1.idMovie , i2.idSubsidiary, i2.idMovie
FROM inventory i1
INNER JOIN inventory i2 ON i1.idMovie = i2.idMovie
WHERE (i1.idSubsidiary= inv1.idSubsidiary and i2.idSubsidiary= inv2.idSubsidiary
AND i2.idSubsidiary IS NULL

)


The result I'm looking for would be something like this:



idSubsidiary | idSubsidiary
---------------------------
0 | 1
3 | 4


So, subsidiary 0 and 1 have the same identical movies on the inventory, same with 3 and 4.



However, the previously shown query is not working. Basically, the query looks up for couples of SubsidiaryID's on Inventory and then runs a nested query to find if the first Subsidiary have any movie that the second Subsidiary does not have. If they don't, it selects both.



However, the nested query is not working. As I said, I want to do a left join of the same table without the inner part.



Any help is much appreciated :)










share|improve this question



















  • 3





    Could you provide with a sample input data and expected output

    – George Joseph
    Nov 15 '18 at 23:21






  • 2





    See meta.stackoverflow.com/questions/333952/…

    – Strawberry
    Nov 15 '18 at 23:44











  • Sorry, I tried my best to make it much clearer now.

    – Luciano Porta
    Nov 16 '18 at 19:36















-2















So, I have an intermediate table in MySQL called "Inventory", with two PKs: idMovie and idSubsidiary. That table looks like this:



----------------------------------
idMovie (int) | idSubsidiary (int)
----------------------------------
0 | 0
2 | 0
1 | 1
3 | 2
----------------------------------


I want to select the IDs of the pair of subsidiaries that have exactly the same movies.



For that, I was thinking about something like this:



select distinct inv1.idSubsidiary, inv2.idSubsidiary
from inventory inv1
join inventory inv2
on inv1.idSubsidiary <> inv2.idSubsidiary
where not exists (

SELECT i1.idSubsidiary, i1.idMovie , i2.idSubsidiary, i2.idMovie
FROM inventory i1
INNER JOIN inventory i2 ON i1.idMovie = i2.idMovie
WHERE (i1.idSubsidiary= inv1.idSubsidiary and i2.idSubsidiary= inv2.idSubsidiary
AND i2.idSubsidiary IS NULL

)


The result I'm looking for would be something like this:



idSubsidiary | idSubsidiary
---------------------------
0 | 1
3 | 4


So, subsidiary 0 and 1 have the same identical movies on the inventory, same with 3 and 4.



However, the previously shown query is not working. Basically, the query looks up for couples of SubsidiaryID's on Inventory and then runs a nested query to find if the first Subsidiary have any movie that the second Subsidiary does not have. If they don't, it selects both.



However, the nested query is not working. As I said, I want to do a left join of the same table without the inner part.



Any help is much appreciated :)










share|improve this question



















  • 3





    Could you provide with a sample input data and expected output

    – George Joseph
    Nov 15 '18 at 23:21






  • 2





    See meta.stackoverflow.com/questions/333952/…

    – Strawberry
    Nov 15 '18 at 23:44











  • Sorry, I tried my best to make it much clearer now.

    – Luciano Porta
    Nov 16 '18 at 19:36













-2












-2








-2








So, I have an intermediate table in MySQL called "Inventory", with two PKs: idMovie and idSubsidiary. That table looks like this:



----------------------------------
idMovie (int) | idSubsidiary (int)
----------------------------------
0 | 0
2 | 0
1 | 1
3 | 2
----------------------------------


I want to select the IDs of the pair of subsidiaries that have exactly the same movies.



For that, I was thinking about something like this:



select distinct inv1.idSubsidiary, inv2.idSubsidiary
from inventory inv1
join inventory inv2
on inv1.idSubsidiary <> inv2.idSubsidiary
where not exists (

SELECT i1.idSubsidiary, i1.idMovie , i2.idSubsidiary, i2.idMovie
FROM inventory i1
INNER JOIN inventory i2 ON i1.idMovie = i2.idMovie
WHERE (i1.idSubsidiary= inv1.idSubsidiary and i2.idSubsidiary= inv2.idSubsidiary
AND i2.idSubsidiary IS NULL

)


The result I'm looking for would be something like this:



idSubsidiary | idSubsidiary
---------------------------
0 | 1
3 | 4


So, subsidiary 0 and 1 have the same identical movies on the inventory, same with 3 and 4.



However, the previously shown query is not working. Basically, the query looks up for couples of SubsidiaryID's on Inventory and then runs a nested query to find if the first Subsidiary have any movie that the second Subsidiary does not have. If they don't, it selects both.



However, the nested query is not working. As I said, I want to do a left join of the same table without the inner part.



Any help is much appreciated :)










share|improve this question
















So, I have an intermediate table in MySQL called "Inventory", with two PKs: idMovie and idSubsidiary. That table looks like this:



----------------------------------
idMovie (int) | idSubsidiary (int)
----------------------------------
0 | 0
2 | 0
1 | 1
3 | 2
----------------------------------


I want to select the IDs of the pair of subsidiaries that have exactly the same movies.



For that, I was thinking about something like this:



select distinct inv1.idSubsidiary, inv2.idSubsidiary
from inventory inv1
join inventory inv2
on inv1.idSubsidiary <> inv2.idSubsidiary
where not exists (

SELECT i1.idSubsidiary, i1.idMovie , i2.idSubsidiary, i2.idMovie
FROM inventory i1
INNER JOIN inventory i2 ON i1.idMovie = i2.idMovie
WHERE (i1.idSubsidiary= inv1.idSubsidiary and i2.idSubsidiary= inv2.idSubsidiary
AND i2.idSubsidiary IS NULL

)


The result I'm looking for would be something like this:



idSubsidiary | idSubsidiary
---------------------------
0 | 1
3 | 4


So, subsidiary 0 and 1 have the same identical movies on the inventory, same with 3 and 4.



However, the previously shown query is not working. Basically, the query looks up for couples of SubsidiaryID's on Inventory and then runs a nested query to find if the first Subsidiary have any movie that the second Subsidiary does not have. If they don't, it selects both.



However, the nested query is not working. As I said, I want to do a left join of the same table without the inner part.



Any help is much appreciated :)







mysql sql database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 19:35







Luciano Porta

















asked Nov 15 '18 at 22:57









Luciano PortaLuciano Porta

12




12







  • 3





    Could you provide with a sample input data and expected output

    – George Joseph
    Nov 15 '18 at 23:21






  • 2





    See meta.stackoverflow.com/questions/333952/…

    – Strawberry
    Nov 15 '18 at 23:44











  • Sorry, I tried my best to make it much clearer now.

    – Luciano Porta
    Nov 16 '18 at 19:36












  • 3





    Could you provide with a sample input data and expected output

    – George Joseph
    Nov 15 '18 at 23:21






  • 2





    See meta.stackoverflow.com/questions/333952/…

    – Strawberry
    Nov 15 '18 at 23:44











  • Sorry, I tried my best to make it much clearer now.

    – Luciano Porta
    Nov 16 '18 at 19:36







3




3





Could you provide with a sample input data and expected output

– George Joseph
Nov 15 '18 at 23:21





Could you provide with a sample input data and expected output

– George Joseph
Nov 15 '18 at 23:21




2




2





See meta.stackoverflow.com/questions/333952/…

– Strawberry
Nov 15 '18 at 23:44





See meta.stackoverflow.com/questions/333952/…

– Strawberry
Nov 15 '18 at 23:44













Sorry, I tried my best to make it much clearer now.

– Luciano Porta
Nov 16 '18 at 19:36





Sorry, I tried my best to make it much clearer now.

– Luciano Porta
Nov 16 '18 at 19:36












1 Answer
1






active

oldest

votes


















0














The simplest method in MySQL is to do double aggregation:



select movies, group_concat(idSubsidiary) as subsidiaries
from (select i.idSubsidiary, group_concat(idMovie order by idMovie) as movies
from inventory i
group by i.idSubsidiary
) s
group by movies
having count(*) > 1;


Each row in the result set is a set of movies with the set of ids that have exactly those movies. Those are the duplicates.






share|improve this answer























  • I want to avoid using count here because there could be two subsidiaries that have 196 movies but those movies aren't necessarily the same.

    – Luciano Porta
    Nov 16 '18 at 19:34










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329051%2fquery-that-selects-ids-of-pair-of-subsidiaries-that-have-exactly-the-same-movies%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The simplest method in MySQL is to do double aggregation:



select movies, group_concat(idSubsidiary) as subsidiaries
from (select i.idSubsidiary, group_concat(idMovie order by idMovie) as movies
from inventory i
group by i.idSubsidiary
) s
group by movies
having count(*) > 1;


Each row in the result set is a set of movies with the set of ids that have exactly those movies. Those are the duplicates.






share|improve this answer























  • I want to avoid using count here because there could be two subsidiaries that have 196 movies but those movies aren't necessarily the same.

    – Luciano Porta
    Nov 16 '18 at 19:34















0














The simplest method in MySQL is to do double aggregation:



select movies, group_concat(idSubsidiary) as subsidiaries
from (select i.idSubsidiary, group_concat(idMovie order by idMovie) as movies
from inventory i
group by i.idSubsidiary
) s
group by movies
having count(*) > 1;


Each row in the result set is a set of movies with the set of ids that have exactly those movies. Those are the duplicates.






share|improve this answer























  • I want to avoid using count here because there could be two subsidiaries that have 196 movies but those movies aren't necessarily the same.

    – Luciano Porta
    Nov 16 '18 at 19:34













0












0








0







The simplest method in MySQL is to do double aggregation:



select movies, group_concat(idSubsidiary) as subsidiaries
from (select i.idSubsidiary, group_concat(idMovie order by idMovie) as movies
from inventory i
group by i.idSubsidiary
) s
group by movies
having count(*) > 1;


Each row in the result set is a set of movies with the set of ids that have exactly those movies. Those are the duplicates.






share|improve this answer













The simplest method in MySQL is to do double aggregation:



select movies, group_concat(idSubsidiary) as subsidiaries
from (select i.idSubsidiary, group_concat(idMovie order by idMovie) as movies
from inventory i
group by i.idSubsidiary
) s
group by movies
having count(*) > 1;


Each row in the result set is a set of movies with the set of ids that have exactly those movies. Those are the duplicates.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 4:27









Gordon LinoffGordon Linoff

786k35310416




786k35310416












  • I want to avoid using count here because there could be two subsidiaries that have 196 movies but those movies aren't necessarily the same.

    – Luciano Porta
    Nov 16 '18 at 19:34

















  • I want to avoid using count here because there could be two subsidiaries that have 196 movies but those movies aren't necessarily the same.

    – Luciano Porta
    Nov 16 '18 at 19:34
















I want to avoid using count here because there could be two subsidiaries that have 196 movies but those movies aren't necessarily the same.

– Luciano Porta
Nov 16 '18 at 19:34





I want to avoid using count here because there could be two subsidiaries that have 196 movies but those movies aren't necessarily the same.

– Luciano Porta
Nov 16 '18 at 19:34



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329051%2fquery-that-selects-ids-of-pair-of-subsidiaries-that-have-exactly-the-same-movies%23new-answer', 'question_page');

);

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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線