MS Access - Grouped query based on MAX field











up vote
3
down vote

favorite












I am trying to build a SQL Query that takes the following data:



+-------------+--------+---------+---------+--------+
| Primary Key | ID | Version | Class | Fruit? |
+-------------+--------+---------+---------+--------+
| 1 | Banana | 1 | NORTH | Yes |
| 2 | Onion | 1 | WEST | No |
| 3 | Orange | 1 | NA | Yes |
| 4 | Orange | 2 | PACIFIC | Yes |
| 5 | Banana | 2 | EUR | Yes |
| 6 | Celery | 1 | EUR | No |
| 7 | Celery | 3 | SOUTH | No |
| 8 | Celery | 4 | SOUTH | No |
| 9 | Pepper | 1 | N-PAC | No |
| 10 | Pepper | 2 | N-PAX | No |
+-------------+--------+---------+---------+--------+


And returns ID of latest version and its corresponding data, where the criteria of Fruit is examined.



An SQL string would be needed to return ID with class for Max version where Fruit = No



Results:



+--------+-------+
| ID | Class |
+--------+-------+
| Onion | NORTH |
| Celery | SOUTH |
| Pepper | N-PAX |
+--------+-------+


I only need to return the ID and it's class to store in an MS Access listbox.



I managed to build a Group By / Max query in the editor and was only able to get IDs to return grouped, but the corresponding data was not associated with the maximum version.



Thanks for your help and expertise.










share|improve this question









New contributor




ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    3
    down vote

    favorite












    I am trying to build a SQL Query that takes the following data:



    +-------------+--------+---------+---------+--------+
    | Primary Key | ID | Version | Class | Fruit? |
    +-------------+--------+---------+---------+--------+
    | 1 | Banana | 1 | NORTH | Yes |
    | 2 | Onion | 1 | WEST | No |
    | 3 | Orange | 1 | NA | Yes |
    | 4 | Orange | 2 | PACIFIC | Yes |
    | 5 | Banana | 2 | EUR | Yes |
    | 6 | Celery | 1 | EUR | No |
    | 7 | Celery | 3 | SOUTH | No |
    | 8 | Celery | 4 | SOUTH | No |
    | 9 | Pepper | 1 | N-PAC | No |
    | 10 | Pepper | 2 | N-PAX | No |
    +-------------+--------+---------+---------+--------+


    And returns ID of latest version and its corresponding data, where the criteria of Fruit is examined.



    An SQL string would be needed to return ID with class for Max version where Fruit = No



    Results:



    +--------+-------+
    | ID | Class |
    +--------+-------+
    | Onion | NORTH |
    | Celery | SOUTH |
    | Pepper | N-PAX |
    +--------+-------+


    I only need to return the ID and it's class to store in an MS Access listbox.



    I managed to build a Group By / Max query in the editor and was only able to get IDs to return grouped, but the corresponding data was not associated with the maximum version.



    Thanks for your help and expertise.










    share|improve this question









    New contributor




    ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I am trying to build a SQL Query that takes the following data:



      +-------------+--------+---------+---------+--------+
      | Primary Key | ID | Version | Class | Fruit? |
      +-------------+--------+---------+---------+--------+
      | 1 | Banana | 1 | NORTH | Yes |
      | 2 | Onion | 1 | WEST | No |
      | 3 | Orange | 1 | NA | Yes |
      | 4 | Orange | 2 | PACIFIC | Yes |
      | 5 | Banana | 2 | EUR | Yes |
      | 6 | Celery | 1 | EUR | No |
      | 7 | Celery | 3 | SOUTH | No |
      | 8 | Celery | 4 | SOUTH | No |
      | 9 | Pepper | 1 | N-PAC | No |
      | 10 | Pepper | 2 | N-PAX | No |
      +-------------+--------+---------+---------+--------+


      And returns ID of latest version and its corresponding data, where the criteria of Fruit is examined.



      An SQL string would be needed to return ID with class for Max version where Fruit = No



      Results:



      +--------+-------+
      | ID | Class |
      +--------+-------+
      | Onion | NORTH |
      | Celery | SOUTH |
      | Pepper | N-PAX |
      +--------+-------+


      I only need to return the ID and it's class to store in an MS Access listbox.



      I managed to build a Group By / Max query in the editor and was only able to get IDs to return grouped, but the corresponding data was not associated with the maximum version.



      Thanks for your help and expertise.










      share|improve this question









      New contributor




      ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am trying to build a SQL Query that takes the following data:



      +-------------+--------+---------+---------+--------+
      | Primary Key | ID | Version | Class | Fruit? |
      +-------------+--------+---------+---------+--------+
      | 1 | Banana | 1 | NORTH | Yes |
      | 2 | Onion | 1 | WEST | No |
      | 3 | Orange | 1 | NA | Yes |
      | 4 | Orange | 2 | PACIFIC | Yes |
      | 5 | Banana | 2 | EUR | Yes |
      | 6 | Celery | 1 | EUR | No |
      | 7 | Celery | 3 | SOUTH | No |
      | 8 | Celery | 4 | SOUTH | No |
      | 9 | Pepper | 1 | N-PAC | No |
      | 10 | Pepper | 2 | N-PAX | No |
      +-------------+--------+---------+---------+--------+


      And returns ID of latest version and its corresponding data, where the criteria of Fruit is examined.



      An SQL string would be needed to return ID with class for Max version where Fruit = No



      Results:



      +--------+-------+
      | ID | Class |
      +--------+-------+
      | Onion | NORTH |
      | Celery | SOUTH |
      | Pepper | N-PAX |
      +--------+-------+


      I only need to return the ID and it's class to store in an MS Access listbox.



      I managed to build a Group By / Max query in the editor and was only able to get IDs to return grouped, but the corresponding data was not associated with the maximum version.



      Thanks for your help and expertise.







      vba database microsoft-access sql






      share|improve this question









      New contributor




      ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 26 at 17:19









      Lee Mac

      428110




      428110






      New contributor




      ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 25 at 0:30









      ExcelMania

      161




      161




      New contributor




      ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      ExcelMania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          There are several ways that you can achieve this.



          The following examples all assume that your table is named table1 and that your fields are id, class, version and fruit (not fruit?) - change these as appropriate to suit your data.



          Note: the following examples assume that your fruit field is a text field. If your fruit field is actually a boolean (yes/no) field, then remove the single quotes around 'No' in the following examples.





          Using a joined subquery:



          select u.id, u.class
          from table1 u inner join
          (
          select t.id, max(t.version) as mv
          from table1 t
          where t.fruit = 'No'
          group by t.id
          ) v on u.id = v.id and u.version = v.mv


          Here, the subquery selects the greatest version for each id for records where fruit = 'No' and this is then joined to the complete dataset to return the required fields for each id and version.





          Using a correlated subquery:



          select t.id, t.class
          from table1 t
          where t.fruit = 'No' and not exists
          (select 1 from table1 u where u.id = t.id and u.fruit = 'No' and u.version > t.version)


          Here, the selection is performed entirely within the WHERE clause, which, for every record, tests whether there exists another record in the set with the same id and a greater version, and if so, the record is not returned.





          Using a LEFT JOIN with unequal join criteria:



          select t.id, t.class
          from table1 t left join table1 u on t.id = u.id and t.version < u.version
          where t.fruit = 'No' and u.id is null


          This example can only be represented in MS Access in the SQL view, as the MS Access Query Designer cannot display joins which have equal join criteria (i.e. where one field equals another).



          This example is similar in operation to the correlated subquery, but the selection is performed by the join, rather than within the WHERE clause.





          Finally, note that your given example result is incorrect: the class for the maximum version for id = 'Onion' should be WEST, not SOUTH.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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',
            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
            });


            }
            });






            ExcelMania is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1378124%2fms-access-grouped-query-based-on-max-field%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








            up vote
            0
            down vote













            There are several ways that you can achieve this.



            The following examples all assume that your table is named table1 and that your fields are id, class, version and fruit (not fruit?) - change these as appropriate to suit your data.



            Note: the following examples assume that your fruit field is a text field. If your fruit field is actually a boolean (yes/no) field, then remove the single quotes around 'No' in the following examples.





            Using a joined subquery:



            select u.id, u.class
            from table1 u inner join
            (
            select t.id, max(t.version) as mv
            from table1 t
            where t.fruit = 'No'
            group by t.id
            ) v on u.id = v.id and u.version = v.mv


            Here, the subquery selects the greatest version for each id for records where fruit = 'No' and this is then joined to the complete dataset to return the required fields for each id and version.





            Using a correlated subquery:



            select t.id, t.class
            from table1 t
            where t.fruit = 'No' and not exists
            (select 1 from table1 u where u.id = t.id and u.fruit = 'No' and u.version > t.version)


            Here, the selection is performed entirely within the WHERE clause, which, for every record, tests whether there exists another record in the set with the same id and a greater version, and if so, the record is not returned.





            Using a LEFT JOIN with unequal join criteria:



            select t.id, t.class
            from table1 t left join table1 u on t.id = u.id and t.version < u.version
            where t.fruit = 'No' and u.id is null


            This example can only be represented in MS Access in the SQL view, as the MS Access Query Designer cannot display joins which have equal join criteria (i.e. where one field equals another).



            This example is similar in operation to the correlated subquery, but the selection is performed by the join, rather than within the WHERE clause.





            Finally, note that your given example result is incorrect: the class for the maximum version for id = 'Onion' should be WEST, not SOUTH.






            share|improve this answer



























              up vote
              0
              down vote













              There are several ways that you can achieve this.



              The following examples all assume that your table is named table1 and that your fields are id, class, version and fruit (not fruit?) - change these as appropriate to suit your data.



              Note: the following examples assume that your fruit field is a text field. If your fruit field is actually a boolean (yes/no) field, then remove the single quotes around 'No' in the following examples.





              Using a joined subquery:



              select u.id, u.class
              from table1 u inner join
              (
              select t.id, max(t.version) as mv
              from table1 t
              where t.fruit = 'No'
              group by t.id
              ) v on u.id = v.id and u.version = v.mv


              Here, the subquery selects the greatest version for each id for records where fruit = 'No' and this is then joined to the complete dataset to return the required fields for each id and version.





              Using a correlated subquery:



              select t.id, t.class
              from table1 t
              where t.fruit = 'No' and not exists
              (select 1 from table1 u where u.id = t.id and u.fruit = 'No' and u.version > t.version)


              Here, the selection is performed entirely within the WHERE clause, which, for every record, tests whether there exists another record in the set with the same id and a greater version, and if so, the record is not returned.





              Using a LEFT JOIN with unequal join criteria:



              select t.id, t.class
              from table1 t left join table1 u on t.id = u.id and t.version < u.version
              where t.fruit = 'No' and u.id is null


              This example can only be represented in MS Access in the SQL view, as the MS Access Query Designer cannot display joins which have equal join criteria (i.e. where one field equals another).



              This example is similar in operation to the correlated subquery, but the selection is performed by the join, rather than within the WHERE clause.





              Finally, note that your given example result is incorrect: the class for the maximum version for id = 'Onion' should be WEST, not SOUTH.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                There are several ways that you can achieve this.



                The following examples all assume that your table is named table1 and that your fields are id, class, version and fruit (not fruit?) - change these as appropriate to suit your data.



                Note: the following examples assume that your fruit field is a text field. If your fruit field is actually a boolean (yes/no) field, then remove the single quotes around 'No' in the following examples.





                Using a joined subquery:



                select u.id, u.class
                from table1 u inner join
                (
                select t.id, max(t.version) as mv
                from table1 t
                where t.fruit = 'No'
                group by t.id
                ) v on u.id = v.id and u.version = v.mv


                Here, the subquery selects the greatest version for each id for records where fruit = 'No' and this is then joined to the complete dataset to return the required fields for each id and version.





                Using a correlated subquery:



                select t.id, t.class
                from table1 t
                where t.fruit = 'No' and not exists
                (select 1 from table1 u where u.id = t.id and u.fruit = 'No' and u.version > t.version)


                Here, the selection is performed entirely within the WHERE clause, which, for every record, tests whether there exists another record in the set with the same id and a greater version, and if so, the record is not returned.





                Using a LEFT JOIN with unequal join criteria:



                select t.id, t.class
                from table1 t left join table1 u on t.id = u.id and t.version < u.version
                where t.fruit = 'No' and u.id is null


                This example can only be represented in MS Access in the SQL view, as the MS Access Query Designer cannot display joins which have equal join criteria (i.e. where one field equals another).



                This example is similar in operation to the correlated subquery, but the selection is performed by the join, rather than within the WHERE clause.





                Finally, note that your given example result is incorrect: the class for the maximum version for id = 'Onion' should be WEST, not SOUTH.






                share|improve this answer














                There are several ways that you can achieve this.



                The following examples all assume that your table is named table1 and that your fields are id, class, version and fruit (not fruit?) - change these as appropriate to suit your data.



                Note: the following examples assume that your fruit field is a text field. If your fruit field is actually a boolean (yes/no) field, then remove the single quotes around 'No' in the following examples.





                Using a joined subquery:



                select u.id, u.class
                from table1 u inner join
                (
                select t.id, max(t.version) as mv
                from table1 t
                where t.fruit = 'No'
                group by t.id
                ) v on u.id = v.id and u.version = v.mv


                Here, the subquery selects the greatest version for each id for records where fruit = 'No' and this is then joined to the complete dataset to return the required fields for each id and version.





                Using a correlated subquery:



                select t.id, t.class
                from table1 t
                where t.fruit = 'No' and not exists
                (select 1 from table1 u where u.id = t.id and u.fruit = 'No' and u.version > t.version)


                Here, the selection is performed entirely within the WHERE clause, which, for every record, tests whether there exists another record in the set with the same id and a greater version, and if so, the record is not returned.





                Using a LEFT JOIN with unequal join criteria:



                select t.id, t.class
                from table1 t left join table1 u on t.id = u.id and t.version < u.version
                where t.fruit = 'No' and u.id is null


                This example can only be represented in MS Access in the SQL view, as the MS Access Query Designer cannot display joins which have equal join criteria (i.e. where one field equals another).



                This example is similar in operation to the correlated subquery, but the selection is performed by the join, rather than within the WHERE clause.





                Finally, note that your given example result is incorrect: the class for the maximum version for id = 'Onion' should be WEST, not SOUTH.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 25 at 12:29

























                answered Nov 25 at 12:14









                Lee Mac

                428110




                428110






















                    ExcelMania is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    ExcelMania is a new contributor. Be nice, and check out our Code of Conduct.













                    ExcelMania is a new contributor. Be nice, and check out our Code of Conduct.












                    ExcelMania is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Super User!


                    • 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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1378124%2fms-access-grouped-query-based-on-max-field%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

                    flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                    Mangá

                     ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕