Neo4j Cypher Sorting and adding results of a list with multiple duplicates












0















So i've got this graph database which has 20 football teams in nodes called "Team", with the only attribute being their name and a constraint ensuring uniqueness. Ive also got over 350 football matches again in nodes called "Match", with the attributes such as the date, team 1 score, team 2 score. The matches nodes have are linked in such a way that team 1 is the home_team and team 2 is the away_team so the relationship is like this:



MERGE (m)-[:away_team]->(t1)
MERGE (m)-[:away_team]->(t2)


So far all of what I mentioned has worked, however now I am trying to display the team with the most wins in January. I managed to get a return with the teams in each match, and their result. It looks like this (the code is below the image)



enter image description here



MATCH (m:Match)-[:home_team]->(home_team:Team), (m:Match)-[:away_team]->(away_team:Team)
WHERE m.date CONTAINS 'Jan'
WITH m as matches, [home_team, away_team] as teams
return teams, collect (matches.home_score) AS home_result, collect (matches.away_score) AS away_result


After getting to this point I got stuck. Im not entirely sure where go from here. The issue is since one team will play multiple matches in January and they can be either home or away so their result is spread between two columns and Im not entirely sure how to extract it and associate the team result to the team name and then display the highest score from the won matches. Any help is greatly appreciated!



P.S. Im new to Cypher/Neo4j










share|improve this question



























    0















    So i've got this graph database which has 20 football teams in nodes called "Team", with the only attribute being their name and a constraint ensuring uniqueness. Ive also got over 350 football matches again in nodes called "Match", with the attributes such as the date, team 1 score, team 2 score. The matches nodes have are linked in such a way that team 1 is the home_team and team 2 is the away_team so the relationship is like this:



    MERGE (m)-[:away_team]->(t1)
    MERGE (m)-[:away_team]->(t2)


    So far all of what I mentioned has worked, however now I am trying to display the team with the most wins in January. I managed to get a return with the teams in each match, and their result. It looks like this (the code is below the image)



    enter image description here



    MATCH (m:Match)-[:home_team]->(home_team:Team), (m:Match)-[:away_team]->(away_team:Team)
    WHERE m.date CONTAINS 'Jan'
    WITH m as matches, [home_team, away_team] as teams
    return teams, collect (matches.home_score) AS home_result, collect (matches.away_score) AS away_result


    After getting to this point I got stuck. Im not entirely sure where go from here. The issue is since one team will play multiple matches in January and they can be either home or away so their result is spread between two columns and Im not entirely sure how to extract it and associate the team result to the team name and then display the highest score from the won matches. Any help is greatly appreciated!



    P.S. Im new to Cypher/Neo4j










    share|improve this question

























      0












      0








      0








      So i've got this graph database which has 20 football teams in nodes called "Team", with the only attribute being their name and a constraint ensuring uniqueness. Ive also got over 350 football matches again in nodes called "Match", with the attributes such as the date, team 1 score, team 2 score. The matches nodes have are linked in such a way that team 1 is the home_team and team 2 is the away_team so the relationship is like this:



      MERGE (m)-[:away_team]->(t1)
      MERGE (m)-[:away_team]->(t2)


      So far all of what I mentioned has worked, however now I am trying to display the team with the most wins in January. I managed to get a return with the teams in each match, and their result. It looks like this (the code is below the image)



      enter image description here



      MATCH (m:Match)-[:home_team]->(home_team:Team), (m:Match)-[:away_team]->(away_team:Team)
      WHERE m.date CONTAINS 'Jan'
      WITH m as matches, [home_team, away_team] as teams
      return teams, collect (matches.home_score) AS home_result, collect (matches.away_score) AS away_result


      After getting to this point I got stuck. Im not entirely sure where go from here. The issue is since one team will play multiple matches in January and they can be either home or away so their result is spread between two columns and Im not entirely sure how to extract it and associate the team result to the team name and then display the highest score from the won matches. Any help is greatly appreciated!



      P.S. Im new to Cypher/Neo4j










      share|improve this question














      So i've got this graph database which has 20 football teams in nodes called "Team", with the only attribute being their name and a constraint ensuring uniqueness. Ive also got over 350 football matches again in nodes called "Match", with the attributes such as the date, team 1 score, team 2 score. The matches nodes have are linked in such a way that team 1 is the home_team and team 2 is the away_team so the relationship is like this:



      MERGE (m)-[:away_team]->(t1)
      MERGE (m)-[:away_team]->(t2)


      So far all of what I mentioned has worked, however now I am trying to display the team with the most wins in January. I managed to get a return with the teams in each match, and their result. It looks like this (the code is below the image)



      enter image description here



      MATCH (m:Match)-[:home_team]->(home_team:Team), (m:Match)-[:away_team]->(away_team:Team)
      WHERE m.date CONTAINS 'Jan'
      WITH m as matches, [home_team, away_team] as teams
      return teams, collect (matches.home_score) AS home_result, collect (matches.away_score) AS away_result


      After getting to this point I got stuck. Im not entirely sure where go from here. The issue is since one team will play multiple matches in January and they can be either home or away so their result is spread between two columns and Im not entirely sure how to extract it and associate the team result to the team name and then display the highest score from the won matches. Any help is greatly appreciated!



      P.S. Im new to Cypher/Neo4j







      neo4j cypher






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 16:02









      AligatorAligator

      444




      444
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Something like this might work, using both relationship types in the MATCH (it will match to both types), then using some boolean logic to filter out the uninteresting results and getting the top team by count:



          MATCH (t:Team)<-[r:home_team|away_team]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          WITH t, type(r) = 'home_team' as isHome, m.home_score > m.away_score as homeWin
          WHERE (isHome AND homeWin) OR (NOT isHome AND NOT homeWin)
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1


          This could be a bit easier if you refactored your graph to add won as a boolean property of the :home_team and :away_team relationships (maybe tie as well, but that's outside of the scope of this query).



          If such a property existed (and was either updated with data import or when scores were updated), then your query here would become simpler:



          MATCH (t:Team)<-[:home_team|away_team {won:true}]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1





          share|improve this answer
























          • Thank you so much for the help! That put me in the right direction!

            – Aligator
            Nov 19 '18 at 19:01











          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%2f53378475%2fneo4j-cypher-sorting-and-adding-results-of-a-list-with-multiple-duplicates%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









          1














          Something like this might work, using both relationship types in the MATCH (it will match to both types), then using some boolean logic to filter out the uninteresting results and getting the top team by count:



          MATCH (t:Team)<-[r:home_team|away_team]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          WITH t, type(r) = 'home_team' as isHome, m.home_score > m.away_score as homeWin
          WHERE (isHome AND homeWin) OR (NOT isHome AND NOT homeWin)
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1


          This could be a bit easier if you refactored your graph to add won as a boolean property of the :home_team and :away_team relationships (maybe tie as well, but that's outside of the scope of this query).



          If such a property existed (and was either updated with data import or when scores were updated), then your query here would become simpler:



          MATCH (t:Team)<-[:home_team|away_team {won:true}]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1





          share|improve this answer
























          • Thank you so much for the help! That put me in the right direction!

            – Aligator
            Nov 19 '18 at 19:01
















          1














          Something like this might work, using both relationship types in the MATCH (it will match to both types), then using some boolean logic to filter out the uninteresting results and getting the top team by count:



          MATCH (t:Team)<-[r:home_team|away_team]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          WITH t, type(r) = 'home_team' as isHome, m.home_score > m.away_score as homeWin
          WHERE (isHome AND homeWin) OR (NOT isHome AND NOT homeWin)
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1


          This could be a bit easier if you refactored your graph to add won as a boolean property of the :home_team and :away_team relationships (maybe tie as well, but that's outside of the scope of this query).



          If such a property existed (and was either updated with data import or when scores were updated), then your query here would become simpler:



          MATCH (t:Team)<-[:home_team|away_team {won:true}]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1





          share|improve this answer
























          • Thank you so much for the help! That put me in the right direction!

            – Aligator
            Nov 19 '18 at 19:01














          1












          1








          1







          Something like this might work, using both relationship types in the MATCH (it will match to both types), then using some boolean logic to filter out the uninteresting results and getting the top team by count:



          MATCH (t:Team)<-[r:home_team|away_team]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          WITH t, type(r) = 'home_team' as isHome, m.home_score > m.away_score as homeWin
          WHERE (isHome AND homeWin) OR (NOT isHome AND NOT homeWin)
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1


          This could be a bit easier if you refactored your graph to add won as a boolean property of the :home_team and :away_team relationships (maybe tie as well, but that's outside of the scope of this query).



          If such a property existed (and was either updated with data import or when scores were updated), then your query here would become simpler:



          MATCH (t:Team)<-[:home_team|away_team {won:true}]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1





          share|improve this answer













          Something like this might work, using both relationship types in the MATCH (it will match to both types), then using some boolean logic to filter out the uninteresting results and getting the top team by count:



          MATCH (t:Team)<-[r:home_team|away_team]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          WITH t, type(r) = 'home_team' as isHome, m.home_score > m.away_score as homeWin
          WHERE (isHome AND homeWin) OR (NOT isHome AND NOT homeWin)
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1


          This could be a bit easier if you refactored your graph to add won as a boolean property of the :home_team and :away_team relationships (maybe tie as well, but that's outside of the scope of this query).



          If such a property existed (and was either updated with data import or when scores were updated), then your query here would become simpler:



          MATCH (t:Team)<-[:home_team|away_team {won:true}]-(m:Match)
          WHERE m.date CONTAINS 'Jan'
          RETURN t, count(*) as matchesWon
          ORDER BY matchesWon DESC
          LIMIT 1






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 16:47









          InverseFalconInverseFalcon

          18.8k21829




          18.8k21829













          • Thank you so much for the help! That put me in the right direction!

            – Aligator
            Nov 19 '18 at 19:01



















          • Thank you so much for the help! That put me in the right direction!

            – Aligator
            Nov 19 '18 at 19:01

















          Thank you so much for the help! That put me in the right direction!

          – Aligator
          Nov 19 '18 at 19:01





          Thank you so much for the help! That put me in the right direction!

          – Aligator
          Nov 19 '18 at 19:01


















          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%2f53378475%2fneo4j-cypher-sorting-and-adding-results-of-a-list-with-multiple-duplicates%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

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?