MYSQL counting rows until last different row

Eduardo

I have an event table in the form:

|  User  |     Event         |   Date  |
| User A | Failed Log In     |   ...   | 
| User A | Failed Log In     |   ...   |
| User B | Failed Log In     |         |
| User A | Successful Log In |         |
| User B | Successful Log In |         |

What I would like to do is to count all the 'failed log ins' for each user since the last 'successful log in' so I can create a view in the form:

|  User  | Failed Log in attempts |
| User A |            2           |
| User B |            1           |

Is this possible to do in SQL? Or do I need to pull this logic out into my app?

Gordon Linoff

SQL tables represent unordered sets, so there is no "last" or "before" unless a column specifies the ordering. So, this answer assumes some sort of eventdatetime column with that information, although an auto incrementing id would work just as well (and possibly better).

I think this does what you want:

select e.user, count(*) as NumFailedAttempts
from events e
where e.event = 'Failed Log In' and
      e.eventdatetime > (select max(e2.eventdatetime)
                         from events e2
                         where e2.user = e.user and e2.event = 'Successful Log In'
                        );

If you want to include zeros (those with no failed logins), then you can left join this to the users table.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy rows until the last row with value ONLY to another workbook

From Dev

MySQL - Counting results for different criteria in case of multiple rows output

From Dev

counting duplicate rows in mysql

From Dev

Grouping and counting rows by value until it changes

From Dev

For each row, compute average for last 20 rows in MySQL

From Dev

MySQL COUNT(*) not counting result rows

From Dev

Counting specific rows mysql + express

From Dev

Hide/show table rows with a specific class until another row with a different class name?

From Dev

PHP MySQL select random rows where a row is different

From Dev

PySpark: counting rows based on current row value

From Dev

Counting rows from different tables in same query

From Dev

Counting rows with different WHERE and GROUP BY to 1 result

From Dev

Counting with pandas (two different numbers at the same row)

From Dev

What to count when counting all rows MySQL

From Dev

Counting/totaling rows in a create view statement, mysql

From Dev

Mysql Query for counting rows with specific days difference

From Dev

What to count when counting all rows MySQL

From Dev

Add rows of values before last row and preserve last row format

From Dev

MySQL Counting different items in one query

From Dev

Mysql - Counting different values of the same field

From Dev

Python excel Row different rows

From Dev

target rows immediately after clicked row until a specific row

From Dev

MySQL Delete last 1300 rows

From Dev

Count rows until value in column changes mysql

From Dev

MySQL - SELECT until fixed number of rows

From Dev

Delete last row java MYSQL

From Dev

Select the second last row in Mysql

From Dev

MySQL POP Last Row and Delete it

From Dev

Get the last inserterd row mysql

Related Related

  1. 1

    Copy rows until the last row with value ONLY to another workbook

  2. 2

    MySQL - Counting results for different criteria in case of multiple rows output

  3. 3

    counting duplicate rows in mysql

  4. 4

    Grouping and counting rows by value until it changes

  5. 5

    For each row, compute average for last 20 rows in MySQL

  6. 6

    MySQL COUNT(*) not counting result rows

  7. 7

    Counting specific rows mysql + express

  8. 8

    Hide/show table rows with a specific class until another row with a different class name?

  9. 9

    PHP MySQL select random rows where a row is different

  10. 10

    PySpark: counting rows based on current row value

  11. 11

    Counting rows from different tables in same query

  12. 12

    Counting rows with different WHERE and GROUP BY to 1 result

  13. 13

    Counting with pandas (two different numbers at the same row)

  14. 14

    What to count when counting all rows MySQL

  15. 15

    Counting/totaling rows in a create view statement, mysql

  16. 16

    Mysql Query for counting rows with specific days difference

  17. 17

    What to count when counting all rows MySQL

  18. 18

    Add rows of values before last row and preserve last row format

  19. 19

    MySQL Counting different items in one query

  20. 20

    Mysql - Counting different values of the same field

  21. 21

    Python excel Row different rows

  22. 22

    target rows immediately after clicked row until a specific row

  23. 23

    MySQL Delete last 1300 rows

  24. 24

    Count rows until value in column changes mysql

  25. 25

    MySQL - SELECT until fixed number of rows

  26. 26

    Delete last row java MYSQL

  27. 27

    Select the second last row in Mysql

  28. 28

    MySQL POP Last Row and Delete it

  29. 29

    Get the last inserterd row mysql

HotTag

Archive