How to count rows of different group of values in one SELECT

ino

I have a table with data that has an indicator as a number, it is not an ID or foreign key. This number is repeated several times in the table.

I need to print all the rows with included incremental row number within the same indicator value, so 1 is there 2 times, 2 is there 3 times, 3 is there 1 time.

Desired output:

name | indicator | rownumber
a        1        1
b        1        2
c        2        1
d        2        2
e        2        3
f        3        1

I have found this solution to count the rows but I do not know how to reset the counter if the indicator is changed.

The query I have so far is but this is incrementally counting the rows

SELECT name, indicator, 
@rownum := @rownum + 1 as row_number
FROM rownumtable
CROSS JOIN (SELECT @rownum := 0) r
ORDER BY name ASC

BUT it prints the row number - see it on SQL Fiddle

name | indicator | row_number
a         1         1
b         1         2
c         2         3
d         2         4
e         2         5
f         3         6

Is there a way how to reset the row_number counter for specific group of same values in the MySQL query?

juergen d
SELECT name, indicator,        
       @rownum := case when @prevIndicator <> indicator then 1 else @rownum + 1 end as rownumber,
       @prevIndicator := indicator
FROM rownumtable
CROSS JOIN (SELECT @rownum := 0, @prevIndicator := 0) r
ORDER BY name ASC

SQLFiddle demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

How to select the rows with maximum values in each group with dplyr?

From Dev

How to count different values into different rows in SQL efficiently?

From Dev

How to select rows with same values in one column but all must be different between each other in another column

From Mysql

How to select query count only once duplicate rows with MYSQL GROUP BY?

From Dev

How to create a sequential count by group that excludes values that are in rows above

From Dev

How to select rows with certain values within a group in R

From Dev

How to COUNT different values without adding to GROUP BY

From Dev

How to group rows, count in one column and do the sum in the other?

From Dev

Select count of different values in mysql

From Dev

How to select rows that have column values that are duplicates in one column but different values in the other?

From Dev

How to group and order rows with different date values in SQL

From Dev

How to select only one set of rows from a group by result table?

From Dev

MySQL get COUNT and SUM on different GROUP BY in one SELECT

From Dev

SQL : how to select different rows where values are equal to an set of values?

From Dev

How to count rows from one column with different value

From Dev

MySQL - Select multiple rows with the same values with one having to be different

From Dev

How can I count different mysql rows in one database request?

From Dev

How to count the number of unique values in a group of rows?

From Dev

How to select multiple count(*) values then group by a specific column

From Dev

Select values from different rows in one row

From Dev

How to group the rows and get the count based on other column values in oracle

From Dev

How to select rows per group by varying values?

From Dev

In Pandas how do I select rows that have a duplicate in one column but different values in another?

From Dev

How to group different rows in one column?

From Dev

How can I count the number of rows in multiple datasets, group them by one column (month), in a Select statement in Bigquery?

From Dev

How to get the mean values per different group of rows with pandas?

From Dev

How to get rid of a group of values if one of the rows shows an NA value?

From Dev

Count rows by group based on values

From Dev

How to select rows values starting by specific letters by group in a python dataframe?

Related Related

  1. 1

    How to select the rows with maximum values in each group with dplyr?

  2. 2

    How to count different values into different rows in SQL efficiently?

  3. 3

    How to select rows with same values in one column but all must be different between each other in another column

  4. 4

    How to select query count only once duplicate rows with MYSQL GROUP BY?

  5. 5

    How to create a sequential count by group that excludes values that are in rows above

  6. 6

    How to select rows with certain values within a group in R

  7. 7

    How to COUNT different values without adding to GROUP BY

  8. 8

    How to group rows, count in one column and do the sum in the other?

  9. 9

    Select count of different values in mysql

  10. 10

    How to select rows that have column values that are duplicates in one column but different values in the other?

  11. 11

    How to group and order rows with different date values in SQL

  12. 12

    How to select only one set of rows from a group by result table?

  13. 13

    MySQL get COUNT and SUM on different GROUP BY in one SELECT

  14. 14

    SQL : how to select different rows where values are equal to an set of values?

  15. 15

    How to count rows from one column with different value

  16. 16

    MySQL - Select multiple rows with the same values with one having to be different

  17. 17

    How can I count different mysql rows in one database request?

  18. 18

    How to count the number of unique values in a group of rows?

  19. 19

    How to select multiple count(*) values then group by a specific column

  20. 20

    Select values from different rows in one row

  21. 21

    How to group the rows and get the count based on other column values in oracle

  22. 22

    How to select rows per group by varying values?

  23. 23

    In Pandas how do I select rows that have a duplicate in one column but different values in another?

  24. 24

    How to group different rows in one column?

  25. 25

    How can I count the number of rows in multiple datasets, group them by one column (month), in a Select statement in Bigquery?

  26. 26

    How to get the mean values per different group of rows with pandas?

  27. 27

    How to get rid of a group of values if one of the rows shows an NA value?

  28. 28

    Count rows by group based on values

  29. 29

    How to select rows values starting by specific letters by group in a python dataframe?

HotTag

Archive