MySQL - Get repeated consecutive rows












-2















I'm trying to figure out how to get repeated consecutive id_question for each id_user in a table. Let's say I have this:



+-----+---------------+---------+--------+
| id | id_question | id_user | value |
+-----+---------------+---------+--------+
| 1 | 1 | 1 | 1000 |
| 2 | 1 | 1 | 2000 |
| 3 | 2 | 2 | 2200 |
| 4 | 2 | 2 | 1100 |
| 5 | 3 | 1 | 3000 |
+---------------------+---------+--------+


I'd like to get the amount of users (id_user) that has the same consecutive question (id_question)



So in that case the output would be:



+-------------+---------+--------+
| id_question | id_user | count |
+-------------+---------+--------+
| 1 | 1 | 2 |
| 2 | 2 | 2 |
+-------------+---------+--------+


Because id_user 1 has to consecutive equals values for id_question, the same for id_user 2.










share|improve this question

























  • there is no amount value 2 in your sample input data

    – Madhur Bhaiya
    Nov 15 '18 at 17:50











  • exactly, that's what the query should count. The amount of repeated in sequence for each user. Amount would be a COUNT from the SELECT statement. I edited my question to Count rather than Amount

    – sudodev
    Nov 15 '18 at 17:53













  • define sequence,, SQL is orderless or sequence less without a column where you can see the order like a auto_increment or datetime it's not possible the define a sequence or get consecutive rows

    – Raymond Nijland
    Nov 15 '18 at 17:55













  • @RaymondNijland It would be consecutive id_question to the same user

    – sudodev
    Nov 15 '18 at 17:56













  • " It would be consecutive id_question to the same user " ok i see and understand now.. What MySQL version are you using?

    – Raymond Nijland
    Nov 15 '18 at 18:00


















-2















I'm trying to figure out how to get repeated consecutive id_question for each id_user in a table. Let's say I have this:



+-----+---------------+---------+--------+
| id | id_question | id_user | value |
+-----+---------------+---------+--------+
| 1 | 1 | 1 | 1000 |
| 2 | 1 | 1 | 2000 |
| 3 | 2 | 2 | 2200 |
| 4 | 2 | 2 | 1100 |
| 5 | 3 | 1 | 3000 |
+---------------------+---------+--------+


I'd like to get the amount of users (id_user) that has the same consecutive question (id_question)



So in that case the output would be:



+-------------+---------+--------+
| id_question | id_user | count |
+-------------+---------+--------+
| 1 | 1 | 2 |
| 2 | 2 | 2 |
+-------------+---------+--------+


Because id_user 1 has to consecutive equals values for id_question, the same for id_user 2.










share|improve this question

























  • there is no amount value 2 in your sample input data

    – Madhur Bhaiya
    Nov 15 '18 at 17:50











  • exactly, that's what the query should count. The amount of repeated in sequence for each user. Amount would be a COUNT from the SELECT statement. I edited my question to Count rather than Amount

    – sudodev
    Nov 15 '18 at 17:53













  • define sequence,, SQL is orderless or sequence less without a column where you can see the order like a auto_increment or datetime it's not possible the define a sequence or get consecutive rows

    – Raymond Nijland
    Nov 15 '18 at 17:55













  • @RaymondNijland It would be consecutive id_question to the same user

    – sudodev
    Nov 15 '18 at 17:56













  • " It would be consecutive id_question to the same user " ok i see and understand now.. What MySQL version are you using?

    – Raymond Nijland
    Nov 15 '18 at 18:00
















-2












-2








-2








I'm trying to figure out how to get repeated consecutive id_question for each id_user in a table. Let's say I have this:



+-----+---------------+---------+--------+
| id | id_question | id_user | value |
+-----+---------------+---------+--------+
| 1 | 1 | 1 | 1000 |
| 2 | 1 | 1 | 2000 |
| 3 | 2 | 2 | 2200 |
| 4 | 2 | 2 | 1100 |
| 5 | 3 | 1 | 3000 |
+---------------------+---------+--------+


I'd like to get the amount of users (id_user) that has the same consecutive question (id_question)



So in that case the output would be:



+-------------+---------+--------+
| id_question | id_user | count |
+-------------+---------+--------+
| 1 | 1 | 2 |
| 2 | 2 | 2 |
+-------------+---------+--------+


Because id_user 1 has to consecutive equals values for id_question, the same for id_user 2.










share|improve this question
















I'm trying to figure out how to get repeated consecutive id_question for each id_user in a table. Let's say I have this:



+-----+---------------+---------+--------+
| id | id_question | id_user | value |
+-----+---------------+---------+--------+
| 1 | 1 | 1 | 1000 |
| 2 | 1 | 1 | 2000 |
| 3 | 2 | 2 | 2200 |
| 4 | 2 | 2 | 1100 |
| 5 | 3 | 1 | 3000 |
+---------------------+---------+--------+


I'd like to get the amount of users (id_user) that has the same consecutive question (id_question)



So in that case the output would be:



+-------------+---------+--------+
| id_question | id_user | count |
+-------------+---------+--------+
| 1 | 1 | 2 |
| 2 | 2 | 2 |
+-------------+---------+--------+


Because id_user 1 has to consecutive equals values for id_question, the same for id_user 2.







mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:20







sudodev

















asked Nov 15 '18 at 17:49









sudodevsudodev

13429




13429













  • there is no amount value 2 in your sample input data

    – Madhur Bhaiya
    Nov 15 '18 at 17:50











  • exactly, that's what the query should count. The amount of repeated in sequence for each user. Amount would be a COUNT from the SELECT statement. I edited my question to Count rather than Amount

    – sudodev
    Nov 15 '18 at 17:53













  • define sequence,, SQL is orderless or sequence less without a column where you can see the order like a auto_increment or datetime it's not possible the define a sequence or get consecutive rows

    – Raymond Nijland
    Nov 15 '18 at 17:55













  • @RaymondNijland It would be consecutive id_question to the same user

    – sudodev
    Nov 15 '18 at 17:56













  • " It would be consecutive id_question to the same user " ok i see and understand now.. What MySQL version are you using?

    – Raymond Nijland
    Nov 15 '18 at 18:00





















  • there is no amount value 2 in your sample input data

    – Madhur Bhaiya
    Nov 15 '18 at 17:50











  • exactly, that's what the query should count. The amount of repeated in sequence for each user. Amount would be a COUNT from the SELECT statement. I edited my question to Count rather than Amount

    – sudodev
    Nov 15 '18 at 17:53













  • define sequence,, SQL is orderless or sequence less without a column where you can see the order like a auto_increment or datetime it's not possible the define a sequence or get consecutive rows

    – Raymond Nijland
    Nov 15 '18 at 17:55













  • @RaymondNijland It would be consecutive id_question to the same user

    – sudodev
    Nov 15 '18 at 17:56













  • " It would be consecutive id_question to the same user " ok i see and understand now.. What MySQL version are you using?

    – Raymond Nijland
    Nov 15 '18 at 18:00



















there is no amount value 2 in your sample input data

– Madhur Bhaiya
Nov 15 '18 at 17:50





there is no amount value 2 in your sample input data

– Madhur Bhaiya
Nov 15 '18 at 17:50













exactly, that's what the query should count. The amount of repeated in sequence for each user. Amount would be a COUNT from the SELECT statement. I edited my question to Count rather than Amount

– sudodev
Nov 15 '18 at 17:53







exactly, that's what the query should count. The amount of repeated in sequence for each user. Amount would be a COUNT from the SELECT statement. I edited my question to Count rather than Amount

– sudodev
Nov 15 '18 at 17:53















define sequence,, SQL is orderless or sequence less without a column where you can see the order like a auto_increment or datetime it's not possible the define a sequence or get consecutive rows

– Raymond Nijland
Nov 15 '18 at 17:55







define sequence,, SQL is orderless or sequence less without a column where you can see the order like a auto_increment or datetime it's not possible the define a sequence or get consecutive rows

– Raymond Nijland
Nov 15 '18 at 17:55















@RaymondNijland It would be consecutive id_question to the same user

– sudodev
Nov 15 '18 at 17:56







@RaymondNijland It would be consecutive id_question to the same user

– sudodev
Nov 15 '18 at 17:56















" It would be consecutive id_question to the same user " ok i see and understand now.. What MySQL version are you using?

– Raymond Nijland
Nov 15 '18 at 18:00







" It would be consecutive id_question to the same user " ok i see and understand now.. What MySQL version are you using?

– Raymond Nijland
Nov 15 '18 at 18:00














1 Answer
1






active

oldest

votes


















0














select id_question, id_user, count(*) as Amount
from yourTable
group by id_question, id_user
having amount > 1





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',
    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%2f53325245%2fmysql-get-repeated-consecutive-rows%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














    select id_question, id_user, count(*) as Amount
    from yourTable
    group by id_question, id_user
    having amount > 1





    share|improve this answer




























      0














      select id_question, id_user, count(*) as Amount
      from yourTable
      group by id_question, id_user
      having amount > 1





      share|improve this answer


























        0












        0








        0







        select id_question, id_user, count(*) as Amount
        from yourTable
        group by id_question, id_user
        having amount > 1





        share|improve this answer













        select id_question, id_user, count(*) as Amount
        from yourTable
        group by id_question, id_user
        having amount > 1






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 17:55









        harryharry

        134115




        134115
































            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%2f53325245%2fmysql-get-repeated-consecutive-rows%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python