sql - error in mysql query, does not return MIN value -


I am trying to work on this query.

This query is called "OMG Ponze" Was written as an answer by:

The exact difference is calculated in the dates of the questions given below, but then fails to choose ROW (ID1-ID2 joints) with the difference is.

  drop table if existing temp4; Create the table temp4, select x.id1, ​​x.id2, x.YEAR, x.mmdd, x.id3, x.id3_yyar, x.id3_MMDD (SELECT t. *, DATEDIFF (CONCAT (t. Id (4)), '-', LEFT (TID 3_MMDD, 2), '-', right (TID 3_MMDD, 2)), conct (CAST (TIEAR AS Cher (4) ), '-', left (TMMDD, 2), '-', right (TMMDD, 2))) In case of FIF, when @ ID1 = TID1 and @ id2 = TID2 then @ RONNUM: = @ Rownum + 1 ELSE @rownum: RK as = 1 END, @ id1: = t.id1, ​​@ id2: = t.id2 to temp3 t zone (SELECT @rownum: = 0, @ id1 : = 0, @ id2: = 0) by R command TID1, TID2, d Iffere, rand ()) x WHERE x.rk = 1;  

I am using the query to randomize each row within each group defined by the ID1-ID2 pair. I should be YEAR-MMDD (i.e. the full difference between YEAR-MMDD and YEAR_ID3-MMDD_ID3 should be reduced) with the minimum difference in the date of ID3. If the exact is greater than one with the same date, then the query must be randomly selected.

If this table was ...

  ID1 ID2 YEAR MMDD ID3 YEAR_ID3 MMDD_ID3 ------------------- -------------------- 1 2 1991 0821 55 1991 0822 1 2 1991 0821 57 1991 0822 1 2 1991 0821 88 1992 0101 1 3 1990 0131 89 2000 0202 1 3 1990 0131 89 2001 0102  

then the query should be returned

  1,2,1991,0821,55 (or 1,2, 19, 1) , 821,57 - according to random draws) 1,3,199,131,8 9  

I am sticking to a SQL dump of a test table ... < / P>

  if the thumbnail is 'samule'; No 'zero', 'year' year (4) no, zero, `mmdd 'four (4) no id 3` `` id' Four (7) no tap, `id 3_aarar` year (4) no tap,` id 3_MMDD four (4) no tap) engine = mice default charset = latin1; Insert `Temp3` values ​​('1', '2', 1992, '0107', '55', 1991, '0528'); INSERT in `Temp3` values ​​('1', '2', 1992; '0107', '57', 1991; '0701'); Insert `Temp` values ​​('1', '3', 1992, '0107', '88', 2000, '0101'); INSERT in `Temp3` values ​​('1', '3', 1992; '0107', '44', 2000, '0101');  

This is a working solution, thanks to OMG token for your help.