I am trying to figure out how I can reset the row numbering in mysql for every other value in this column. Probably best explained with an example:
I have a set of users who make client visits, each client can be visited more than once, and my table records the date of the visit (but not whether it was the first, second, third, etc.). So my table looks like this:
clientid visitdate
100 10-apr-2012
101 15-apr-2012
101 25-apr-2012
102 26-apr-2012
100 28-apr-2012
What I'm looking for is the number (for example) of the second visits over a given period of time. Thus, in the above data there are 3 first visits and 2 second visits.
I assume that I need to use something like the @rownum function, for example:
SELECT visitdate, clientid @rownum: = @rownum + 1 as a string from a visit, (SELECT @rownum: = 0) a ORDER BY clientid, visitdate
which gives me:
clientid visitdate row
100 10-apr-2012 1
100 28-apr-2012 2
101 15-apr-2012 3
101 25-apr-2012 4
102 26-apr-2012 5
but I'm really looking for:
clientid visitdate row
100 10-apr-2012 1
100 28-apr-2012 2
101 15-apr-2012 1
101 25-apr-2012 2
102 26-apr-2012 1
I am having trouble figuring out how to reset the row counter for each client.
Maybe I approach this wrong and I can do these calculations in the application code (PHP), but it feels like something that should be achievable in db.
I saw approaches for SQLServer (for example: http://www.sqlmonster.com/Uwe/Forum.aspx/sql-server-programming/74891/Auto-number-and-reset-based-on-data-value-in- a ) but it doesn't seem to work for mysql?
Any help / suggestions really appreciated, cheers, Alex