sql - fix mysql query to return random row within subgroup -


I am using the following query so that from a subset of a line from Brestimi, records of each ID 1-ID2 pair Minimum distance in time (YEAR and MMDD fields)

  Select as template temp4 * Template Group ID1, ID2 order DADIFF (Conakat (year, '-', left (MCDD, 2), '-', right (MMDD, 2)), CONCAT (ID3_Air, '-', left (ID 3_MMDD, 2), '-', right (ID 3_MMDD, 2)) ASC, RAND () Limited 0, 1;  

From the previous question I posted here, how this table looks

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

For each ID1-ID2 pair, I will have to select ID3 with

The minimum distance between the rules of time (both in the year and MMDD, i mean YEAR And MMDD vs. YEAR_ID3 and MMDD_ID3)

If the maximum ID3 sensor has the following minimum requirements (i.e. both have the same YEAR_ID3 and MMDD_ID3 in both), I need to select a candlelight.

In the above example, the search must be returned

  1, 2,191,0821,55 (or 1,2, 19, 1, 0, 8, 21, 5 7 - according to random draws) 1,3,199,0131,8 9  

VA pasteed ABO VE returns only one line ...: (

Some people had a great solution under the comments section posted by OMG ... but it disappeared?!?!?

I I'm sticking to it

  drop table if existing temp4; Select table temp4 x.id1, ​​x.id2, x.YEAR, x.mmdd, x.id3, x.id3_yyar, xi From d3_MMDD (SELECT t., ABS (AB) (as DATEDIFF (CONCAT (CA. (3), '-', LEFT (TID 3_MMDD, 2), '-', Wright (T. ID 3_MMDD, 2)), CONCAT (CAST (TIEAR AS Cher (4)), '-', left (TMMDD, 2), '-', right (TMMDD, 2))) In case of FIF, When @ id1 = tid1 and @ id2 = tid2 then @rionum: = @ rownum + 1 ELSE @rownum: = 1nd, as RK, @ id1: = t.id1, ​​@ id2: = T.id2 to temp3 t zone (SELECT @rownum: = 0, @ id1: = 0, @ id2: = 0) by R command TID1, TID2, defray, rand ()) x WHERE x .rk = 1;  

I'm sticking a SQL dump of a test table here

  If the drop table is the current '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');  

The usual way to remove a random record from a query

SELECT [] range to range () range []


Comments