MySQL - Get repeated consecutive rows
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
|
show 8 more comments
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
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 aCOUNT
from the SELECT statement. I edited my question toCount
rather thanAmount
– 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
|
show 8 more comments
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
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
mysql
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 aCOUNT
from the SELECT statement. I edited my question toCount
rather thanAmount
– 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
|
show 8 more comments
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 aCOUNT
from the SELECT statement. I edited my question toCount
rather thanAmount
– 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
|
show 8 more comments
1 Answer
1
active
oldest
votes
select id_question, id_user, count(*) as Amount
from yourTable
group by id_question, id_user
having amount > 1
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
select id_question, id_user, count(*) as Amount
from yourTable
group by id_question, id_user
having amount > 1
add a comment |
select id_question, id_user, count(*) as Amount
from yourTable
group by id_question, id_user
having amount > 1
add a comment |
select id_question, id_user, count(*) as Amount
from yourTable
group by id_question, id_user
having amount > 1
select id_question, id_user, count(*) as Amount
from yourTable
group by id_question, id_user
having amount > 1
answered Nov 15 '18 at 17:55
harryharry
134115
134115
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 aCOUNT
from the SELECT statement. I edited my question toCount
rather thanAmount
– 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