MYSQL Group By with value > 0











up vote
0
down vote

favorite












I've been working on this query and it's working as intended by displaying one output per p.ID since I'm grouping by p.ID (since there would be multiple p.ID values without the grouping.) However, depending on the p.ID, I will sometimes get a value of e.alternateEventDurationInMilliseconds that is not the fastest e.alternateEventDurationInMilliseconds.



Is there a way I could still use the GROUP BY p.ID but also add a check to get the minimum e.alternateEventDurationInMilliseconds as long as the duration is still > 0? Otherwise, I'll be given a random value by default that is greater than 0, but I want to specifically get the minimum duration that is greater than 0, with only one row per p.ID.



Thanks! :)



SELECT
p.ID,
e.personaId,
e.EVENTID,
e.alternateEventDurationInMilliseconds
FROM
EVENT_DATA e
INNER JOIN
PERSONA p ON e.personaId = p.ID
WHERE
e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > '0'
GROUP BY
p.ID
ORDER BY
e.`alternateEventDurationInMilliseconds` ASC









share|improve this question
























  • can you please provide your expected output in table format and also which version of mysql you are using
    – fa06
    Nov 8 at 4:59










  • I think you are after the MIN() aggregate method. But please, provide a sample of data and expected output.
    – D. Smania
    Nov 8 at 5:07










  • @fa06 Hopefully this helps visualize what I'm trying to explain. The output will look like this. With the GROUP BY in my query, I will get one value per p.ID (which is what I want). However, in that same image on the bottom two rows, that is what is shown if I take out the GROUP BY. So, you can see that Person1 actually has a faster duration, but it's not shown with the GROUP BY since it's grouping by any time > 0 instead of also grouping by the lowest time > 0. imgur.com/a/bqj4cKp
    – ccc
    Nov 8 at 5:08












  • Your query is invalid SQL and it is rejected by most RDBMS-es except MySQL. Read this answer for an explanation why it is invalid and how MySQL handles it and why it returns values that do not match your expectations.
    – axiac
    Nov 8 at 5:34















up vote
0
down vote

favorite












I've been working on this query and it's working as intended by displaying one output per p.ID since I'm grouping by p.ID (since there would be multiple p.ID values without the grouping.) However, depending on the p.ID, I will sometimes get a value of e.alternateEventDurationInMilliseconds that is not the fastest e.alternateEventDurationInMilliseconds.



Is there a way I could still use the GROUP BY p.ID but also add a check to get the minimum e.alternateEventDurationInMilliseconds as long as the duration is still > 0? Otherwise, I'll be given a random value by default that is greater than 0, but I want to specifically get the minimum duration that is greater than 0, with only one row per p.ID.



Thanks! :)



SELECT
p.ID,
e.personaId,
e.EVENTID,
e.alternateEventDurationInMilliseconds
FROM
EVENT_DATA e
INNER JOIN
PERSONA p ON e.personaId = p.ID
WHERE
e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > '0'
GROUP BY
p.ID
ORDER BY
e.`alternateEventDurationInMilliseconds` ASC









share|improve this question
























  • can you please provide your expected output in table format and also which version of mysql you are using
    – fa06
    Nov 8 at 4:59










  • I think you are after the MIN() aggregate method. But please, provide a sample of data and expected output.
    – D. Smania
    Nov 8 at 5:07










  • @fa06 Hopefully this helps visualize what I'm trying to explain. The output will look like this. With the GROUP BY in my query, I will get one value per p.ID (which is what I want). However, in that same image on the bottom two rows, that is what is shown if I take out the GROUP BY. So, you can see that Person1 actually has a faster duration, but it's not shown with the GROUP BY since it's grouping by any time > 0 instead of also grouping by the lowest time > 0. imgur.com/a/bqj4cKp
    – ccc
    Nov 8 at 5:08












  • Your query is invalid SQL and it is rejected by most RDBMS-es except MySQL. Read this answer for an explanation why it is invalid and how MySQL handles it and why it returns values that do not match your expectations.
    – axiac
    Nov 8 at 5:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've been working on this query and it's working as intended by displaying one output per p.ID since I'm grouping by p.ID (since there would be multiple p.ID values without the grouping.) However, depending on the p.ID, I will sometimes get a value of e.alternateEventDurationInMilliseconds that is not the fastest e.alternateEventDurationInMilliseconds.



Is there a way I could still use the GROUP BY p.ID but also add a check to get the minimum e.alternateEventDurationInMilliseconds as long as the duration is still > 0? Otherwise, I'll be given a random value by default that is greater than 0, but I want to specifically get the minimum duration that is greater than 0, with only one row per p.ID.



Thanks! :)



SELECT
p.ID,
e.personaId,
e.EVENTID,
e.alternateEventDurationInMilliseconds
FROM
EVENT_DATA e
INNER JOIN
PERSONA p ON e.personaId = p.ID
WHERE
e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > '0'
GROUP BY
p.ID
ORDER BY
e.`alternateEventDurationInMilliseconds` ASC









share|improve this question















I've been working on this query and it's working as intended by displaying one output per p.ID since I'm grouping by p.ID (since there would be multiple p.ID values without the grouping.) However, depending on the p.ID, I will sometimes get a value of e.alternateEventDurationInMilliseconds that is not the fastest e.alternateEventDurationInMilliseconds.



Is there a way I could still use the GROUP BY p.ID but also add a check to get the minimum e.alternateEventDurationInMilliseconds as long as the duration is still > 0? Otherwise, I'll be given a random value by default that is greater than 0, but I want to specifically get the minimum duration that is greater than 0, with only one row per p.ID.



Thanks! :)



SELECT
p.ID,
e.personaId,
e.EVENTID,
e.alternateEventDurationInMilliseconds
FROM
EVENT_DATA e
INNER JOIN
PERSONA p ON e.personaId = p.ID
WHERE
e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > '0'
GROUP BY
p.ID
ORDER BY
e.`alternateEventDurationInMilliseconds` ASC






mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 5:26









axiac

42.3k64362




42.3k64362










asked Nov 8 at 4:57









ccc

135




135












  • can you please provide your expected output in table format and also which version of mysql you are using
    – fa06
    Nov 8 at 4:59










  • I think you are after the MIN() aggregate method. But please, provide a sample of data and expected output.
    – D. Smania
    Nov 8 at 5:07










  • @fa06 Hopefully this helps visualize what I'm trying to explain. The output will look like this. With the GROUP BY in my query, I will get one value per p.ID (which is what I want). However, in that same image on the bottom two rows, that is what is shown if I take out the GROUP BY. So, you can see that Person1 actually has a faster duration, but it's not shown with the GROUP BY since it's grouping by any time > 0 instead of also grouping by the lowest time > 0. imgur.com/a/bqj4cKp
    – ccc
    Nov 8 at 5:08












  • Your query is invalid SQL and it is rejected by most RDBMS-es except MySQL. Read this answer for an explanation why it is invalid and how MySQL handles it and why it returns values that do not match your expectations.
    – axiac
    Nov 8 at 5:34


















  • can you please provide your expected output in table format and also which version of mysql you are using
    – fa06
    Nov 8 at 4:59










  • I think you are after the MIN() aggregate method. But please, provide a sample of data and expected output.
    – D. Smania
    Nov 8 at 5:07










  • @fa06 Hopefully this helps visualize what I'm trying to explain. The output will look like this. With the GROUP BY in my query, I will get one value per p.ID (which is what I want). However, in that same image on the bottom two rows, that is what is shown if I take out the GROUP BY. So, you can see that Person1 actually has a faster duration, but it's not shown with the GROUP BY since it's grouping by any time > 0 instead of also grouping by the lowest time > 0. imgur.com/a/bqj4cKp
    – ccc
    Nov 8 at 5:08












  • Your query is invalid SQL and it is rejected by most RDBMS-es except MySQL. Read this answer for an explanation why it is invalid and how MySQL handles it and why it returns values that do not match your expectations.
    – axiac
    Nov 8 at 5:34
















can you please provide your expected output in table format and also which version of mysql you are using
– fa06
Nov 8 at 4:59




can you please provide your expected output in table format and also which version of mysql you are using
– fa06
Nov 8 at 4:59












I think you are after the MIN() aggregate method. But please, provide a sample of data and expected output.
– D. Smania
Nov 8 at 5:07




I think you are after the MIN() aggregate method. But please, provide a sample of data and expected output.
– D. Smania
Nov 8 at 5:07












@fa06 Hopefully this helps visualize what I'm trying to explain. The output will look like this. With the GROUP BY in my query, I will get one value per p.ID (which is what I want). However, in that same image on the bottom two rows, that is what is shown if I take out the GROUP BY. So, you can see that Person1 actually has a faster duration, but it's not shown with the GROUP BY since it's grouping by any time > 0 instead of also grouping by the lowest time > 0. imgur.com/a/bqj4cKp
– ccc
Nov 8 at 5:08






@fa06 Hopefully this helps visualize what I'm trying to explain. The output will look like this. With the GROUP BY in my query, I will get one value per p.ID (which is what I want). However, in that same image on the bottom two rows, that is what is shown if I take out the GROUP BY. So, you can see that Person1 actually has a faster duration, but it's not shown with the GROUP BY since it's grouping by any time > 0 instead of also grouping by the lowest time > 0. imgur.com/a/bqj4cKp
– ccc
Nov 8 at 5:08














Your query is invalid SQL and it is rejected by most RDBMS-es except MySQL. Read this answer for an explanation why it is invalid and how MySQL handles it and why it returns values that do not match your expectations.
– axiac
Nov 8 at 5:34




Your query is invalid SQL and it is rejected by most RDBMS-es except MySQL. Read this answer for an explanation why it is invalid and how MySQL handles it and why it returns values that do not match your expectations.
– axiac
Nov 8 at 5:34












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










You have to use the MIN() aggregate function to select the minimun value (of a particular column) of each group after grouping. Also, why you need to join table PERSONA if you only are retrieving the ID from that table, but this ID is already part of the table EVENT_DATA (on column personaId). The query you may need is this one:



SELECT
p.ID,
MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
FROM
EVENT_DATA e
INNER JOIN
PERSONA p ON e.personaId = p.ID
WHERE
e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
GROUP BY
p.ID
ORDER BY
minAlternateEventDurationInMilliseconds ASC


If you don't need other data from table PERSONA with the exception of the ID, previous query could be simplified to this:



SELECT
MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
FROM
EVENT_DATA e
WHERE
e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
GROUP BY
e.personaId
ORDER BY
minAlternateEventDurationInMilliseconds ASC





share|improve this answer






























    up vote
    1
    down vote













    You'll have to make a new table in a subquery and then join to that table.



    This is the query that will give you the e.id and min (but not zero) data you're looking for:



    select id, personalID, EVENTID,
    min(alternateEventDurationInMilliseconds) as mill,
    from EVENT_DATA
    where alternateEventDurationInMilliseconds > 0
    and e.EVENTID=43
    group by personaId;


    so you should use that table that is created by the subquery as a joined table instead of joining to EVENT_DATE, something like:



    join ( select id, personalID, e.EVENTID,
    min(alternateEventDurationInMilliseconds) as mill,
    from EVENT_DATA
    where alternateEventDurationInMilliseconds > 0
    and e.EVENTID=43
    group by personaId ) e...


    I think that should work.



    This may also work, but I'm not sure - just take your previous query and change e.alternateEventDurationInMilliseconds to "min(e.alternateEventDurationInMilliseconds) as e.alternateEventDurationInMilliseconds" to your original query.






    share|improve this answer




























      up vote
      0
      down vote













      You can use this: (assuming mysql)



      SELECT p.ID, e.alternateEventDurationInMilliseconds from persona p
      join (select min(alternateEventDurationInMilliseconds) as alternateEventDurationInMilliseconds, personaid
      from event_data where personaid = '43' and alternateEventDurationInMilliseconds > 0
      group by personaid) as e on e.personaid = p.Id;


      This is a famous problem:



      The Rows Holding the Group-wise Maximum of a Certain Column






      share|improve this answer





















        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',
        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%2f53201777%2fmysql-group-by-with-value-0%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        You have to use the MIN() aggregate function to select the minimun value (of a particular column) of each group after grouping. Also, why you need to join table PERSONA if you only are retrieving the ID from that table, but this ID is already part of the table EVENT_DATA (on column personaId). The query you may need is this one:



        SELECT
        p.ID,
        MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
        MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
        MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
        FROM
        EVENT_DATA e
        INNER JOIN
        PERSONA p ON e.personaId = p.ID
        WHERE
        e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
        GROUP BY
        p.ID
        ORDER BY
        minAlternateEventDurationInMilliseconds ASC


        If you don't need other data from table PERSONA with the exception of the ID, previous query could be simplified to this:



        SELECT
        MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
        MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
        MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
        FROM
        EVENT_DATA e
        WHERE
        e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
        GROUP BY
        e.personaId
        ORDER BY
        minAlternateEventDurationInMilliseconds ASC





        share|improve this answer



























          up vote
          1
          down vote



          accepted










          You have to use the MIN() aggregate function to select the minimun value (of a particular column) of each group after grouping. Also, why you need to join table PERSONA if you only are retrieving the ID from that table, but this ID is already part of the table EVENT_DATA (on column personaId). The query you may need is this one:



          SELECT
          p.ID,
          MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
          MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
          MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
          FROM
          EVENT_DATA e
          INNER JOIN
          PERSONA p ON e.personaId = p.ID
          WHERE
          e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
          GROUP BY
          p.ID
          ORDER BY
          minAlternateEventDurationInMilliseconds ASC


          If you don't need other data from table PERSONA with the exception of the ID, previous query could be simplified to this:



          SELECT
          MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
          MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
          MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
          FROM
          EVENT_DATA e
          WHERE
          e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
          GROUP BY
          e.personaId
          ORDER BY
          minAlternateEventDurationInMilliseconds ASC





          share|improve this answer

























            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            You have to use the MIN() aggregate function to select the minimun value (of a particular column) of each group after grouping. Also, why you need to join table PERSONA if you only are retrieving the ID from that table, but this ID is already part of the table EVENT_DATA (on column personaId). The query you may need is this one:



            SELECT
            p.ID,
            MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
            MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
            MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
            FROM
            EVENT_DATA e
            INNER JOIN
            PERSONA p ON e.personaId = p.ID
            WHERE
            e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
            GROUP BY
            p.ID
            ORDER BY
            minAlternateEventDurationInMilliseconds ASC


            If you don't need other data from table PERSONA with the exception of the ID, previous query could be simplified to this:



            SELECT
            MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
            MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
            MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
            FROM
            EVENT_DATA e
            WHERE
            e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
            GROUP BY
            e.personaId
            ORDER BY
            minAlternateEventDurationInMilliseconds ASC





            share|improve this answer














            You have to use the MIN() aggregate function to select the minimun value (of a particular column) of each group after grouping. Also, why you need to join table PERSONA if you only are retrieving the ID from that table, but this ID is already part of the table EVENT_DATA (on column personaId). The query you may need is this one:



            SELECT
            p.ID,
            MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
            MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
            MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
            FROM
            EVENT_DATA e
            INNER JOIN
            PERSONA p ON e.personaId = p.ID
            WHERE
            e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
            GROUP BY
            p.ID
            ORDER BY
            minAlternateEventDurationInMilliseconds ASC


            If you don't need other data from table PERSONA with the exception of the ID, previous query could be simplified to this:



            SELECT
            MIN(e.personaId), -- This could be replaced by ANY_VALUE(e.personaId).
            MIN(e.EVENTID), -- This could be replaced by ANY_VALUE(e.EVENTID).
            MIN(e.alternateEventDurationInMilliseconds) AS minAlternateEventDurationInMilliseconds
            FROM
            EVENT_DATA e
            WHERE
            e.EVENTID = '43' AND e.alternateEventDurationInMilliseconds > 0
            GROUP BY
            e.personaId
            ORDER BY
            minAlternateEventDurationInMilliseconds ASC






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 5:35

























            answered Nov 8 at 5:27









            D. Smania

            2,6651321




            2,6651321
























                up vote
                1
                down vote













                You'll have to make a new table in a subquery and then join to that table.



                This is the query that will give you the e.id and min (but not zero) data you're looking for:



                select id, personalID, EVENTID,
                min(alternateEventDurationInMilliseconds) as mill,
                from EVENT_DATA
                where alternateEventDurationInMilliseconds > 0
                and e.EVENTID=43
                group by personaId;


                so you should use that table that is created by the subquery as a joined table instead of joining to EVENT_DATE, something like:



                join ( select id, personalID, e.EVENTID,
                min(alternateEventDurationInMilliseconds) as mill,
                from EVENT_DATA
                where alternateEventDurationInMilliseconds > 0
                and e.EVENTID=43
                group by personaId ) e...


                I think that should work.



                This may also work, but I'm not sure - just take your previous query and change e.alternateEventDurationInMilliseconds to "min(e.alternateEventDurationInMilliseconds) as e.alternateEventDurationInMilliseconds" to your original query.






                share|improve this answer

























                  up vote
                  1
                  down vote













                  You'll have to make a new table in a subquery and then join to that table.



                  This is the query that will give you the e.id and min (but not zero) data you're looking for:



                  select id, personalID, EVENTID,
                  min(alternateEventDurationInMilliseconds) as mill,
                  from EVENT_DATA
                  where alternateEventDurationInMilliseconds > 0
                  and e.EVENTID=43
                  group by personaId;


                  so you should use that table that is created by the subquery as a joined table instead of joining to EVENT_DATE, something like:



                  join ( select id, personalID, e.EVENTID,
                  min(alternateEventDurationInMilliseconds) as mill,
                  from EVENT_DATA
                  where alternateEventDurationInMilliseconds > 0
                  and e.EVENTID=43
                  group by personaId ) e...


                  I think that should work.



                  This may also work, but I'm not sure - just take your previous query and change e.alternateEventDurationInMilliseconds to "min(e.alternateEventDurationInMilliseconds) as e.alternateEventDurationInMilliseconds" to your original query.






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You'll have to make a new table in a subquery and then join to that table.



                    This is the query that will give you the e.id and min (but not zero) data you're looking for:



                    select id, personalID, EVENTID,
                    min(alternateEventDurationInMilliseconds) as mill,
                    from EVENT_DATA
                    where alternateEventDurationInMilliseconds > 0
                    and e.EVENTID=43
                    group by personaId;


                    so you should use that table that is created by the subquery as a joined table instead of joining to EVENT_DATE, something like:



                    join ( select id, personalID, e.EVENTID,
                    min(alternateEventDurationInMilliseconds) as mill,
                    from EVENT_DATA
                    where alternateEventDurationInMilliseconds > 0
                    and e.EVENTID=43
                    group by personaId ) e...


                    I think that should work.



                    This may also work, but I'm not sure - just take your previous query and change e.alternateEventDurationInMilliseconds to "min(e.alternateEventDurationInMilliseconds) as e.alternateEventDurationInMilliseconds" to your original query.






                    share|improve this answer












                    You'll have to make a new table in a subquery and then join to that table.



                    This is the query that will give you the e.id and min (but not zero) data you're looking for:



                    select id, personalID, EVENTID,
                    min(alternateEventDurationInMilliseconds) as mill,
                    from EVENT_DATA
                    where alternateEventDurationInMilliseconds > 0
                    and e.EVENTID=43
                    group by personaId;


                    so you should use that table that is created by the subquery as a joined table instead of joining to EVENT_DATE, something like:



                    join ( select id, personalID, e.EVENTID,
                    min(alternateEventDurationInMilliseconds) as mill,
                    from EVENT_DATA
                    where alternateEventDurationInMilliseconds > 0
                    and e.EVENTID=43
                    group by personaId ) e...


                    I think that should work.



                    This may also work, but I'm not sure - just take your previous query and change e.alternateEventDurationInMilliseconds to "min(e.alternateEventDurationInMilliseconds) as e.alternateEventDurationInMilliseconds" to your original query.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 8 at 5:13









                    iateadonut

                    546513




                    546513






















                        up vote
                        0
                        down vote













                        You can use this: (assuming mysql)



                        SELECT p.ID, e.alternateEventDurationInMilliseconds from persona p
                        join (select min(alternateEventDurationInMilliseconds) as alternateEventDurationInMilliseconds, personaid
                        from event_data where personaid = '43' and alternateEventDurationInMilliseconds > 0
                        group by personaid) as e on e.personaid = p.Id;


                        This is a famous problem:



                        The Rows Holding the Group-wise Maximum of a Certain Column






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          You can use this: (assuming mysql)



                          SELECT p.ID, e.alternateEventDurationInMilliseconds from persona p
                          join (select min(alternateEventDurationInMilliseconds) as alternateEventDurationInMilliseconds, personaid
                          from event_data where personaid = '43' and alternateEventDurationInMilliseconds > 0
                          group by personaid) as e on e.personaid = p.Id;


                          This is a famous problem:



                          The Rows Holding the Group-wise Maximum of a Certain Column






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can use this: (assuming mysql)



                            SELECT p.ID, e.alternateEventDurationInMilliseconds from persona p
                            join (select min(alternateEventDurationInMilliseconds) as alternateEventDurationInMilliseconds, personaid
                            from event_data where personaid = '43' and alternateEventDurationInMilliseconds > 0
                            group by personaid) as e on e.personaid = p.Id;


                            This is a famous problem:



                            The Rows Holding the Group-wise Maximum of a Certain Column






                            share|improve this answer












                            You can use this: (assuming mysql)



                            SELECT p.ID, e.alternateEventDurationInMilliseconds from persona p
                            join (select min(alternateEventDurationInMilliseconds) as alternateEventDurationInMilliseconds, personaid
                            from event_data where personaid = '43' and alternateEventDurationInMilliseconds > 0
                            group by personaid) as e on e.personaid = p.Id;


                            This is a famous problem:



                            The Rows Holding the Group-wise Maximum of a Certain Column







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 8 at 5:09









                            Gauravsa

                            1,6501816




                            1,6501816






























                                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.





                                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%2fstackoverflow.com%2fquestions%2f53201777%2fmysql-group-by-with-value-0%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







                                這個網誌中的熱門文章

                                Academy of Television Arts & Sciences

                                L'Équipe

                                1995 France bombings