Mysql limit rows per group weird results -


I wanted to get the latest 4 dates for each symbolid . I've optimized the code as follows:

  set @num: = 0, @symbolid: = ''; Row_number as symbolid, date, @num: = if (@symbolid = symbolid, @num + 1, 1), @symbolid: = symbolid From the processing group as symbols of the Dame, having the date row_number & lt ; 5   

and get the following results:

  symbolic date line_Number Dummy 1 '2011-09-01 00:00:00' 1 ​​1 1 '2011 -08-31 00:00:00' 3 1 1 '2011-08-30 00:00:00' 5 1 2 '2011-09-01 00:00:00' 1 ​​2 2 '2011-08- 31 00: 00: 00 '3 2 2' 2011-08-30 00:00:00 '5 2 3' 2011-09-01 00:00:00 '1 3 3' 2011-08-31 00:00: 00 '3 3 3' 2011-08-30 00:00:00 '5 3 4' 2011-09-01 00:00:00 '1 4 ...   

The obvious question is, why did I get only 3 rows per code , and why were they counted 1,3,5 numbers? Some details:

  1. I tried to force both of them to an index and not (as seen here), and the same result was found in both ways.
  2. The dates are correct i.e., the listing accurately reflects the symbolic top 3 dates, but the row_number value is closed
  3. when I do not "have" Use the statement, the line number is correct, that is, the most recent date is 1, the next most recent 2, etc.

    Obviously row_number

    I know that I just give "row_number and lt; 7" (like 6 5), but it is very ugly and wants to know what to do to "behave" it .

    I'm not 100% sure why it behaves like this (probably because it is Logically, SELECT has been processed before ORDER BY ), but it should be expected to work as expected:

      SELECT * FROM (select symbolid, date, @num: = if (@symbolid = symbolid, @num + 1, 1) as row_number, @symbolid: = symbolid as dummy from the projection ININER JOIN (SELECT @symbolid: = 0) C INNER JOIN (SELECT @num: = 0) BY DIRECTION BY DI GROW, DEKAY) WHERE row_number & lt; 5    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -