Mysql limit rows per group weird results -
I wanted to get the latest 4 dates for each and get the following results: The obvious question is, why did I get only 3 rows per code , and why were they counted 1,3,5 numbers? Some details: Obviously 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, 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
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 ...
symbolic top 3 dates, but the row_number value is closed
row_number
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
Post a Comment