mysql查询错误,不返回MIN值

时间:2010-09-26 05:39:10

标签: mysql sql

我正试图让这个查询起作用。

查询由“OMG Ponies”编写,作为对fix mysql query to return random row within subgroup

的回答

下面的查询正确计算了日期的差异,但随后无法选择具有该差异最小值的ROW(在ID1-ID2对中)。

  DROP TABLE IF EXISTS temp4;
    CREATE TABLE temp4 AS
    SELECT x.id1,
           x.id2,
           x.YEAR,
           x.MMDD,
           x.id3,
           x.id3_YEAR,
           x.id3_MMDD
     FROM (SELECT t.*,
                   ABS(DATEDIFF(CONCAT(CAST(t.id3_YEAR AS CHAR(4)),'-', LEFT(t.id3_MMDD,2),'-',RIGHT(t.id3_MMDD,2)),
                            CONCAT(CAST(t.YEAR AS CHAR(4)),'-', LEFT(t.MMDD,2),'-',RIGHT(t.MMDD,2))))  AS diff,
                   CASE 
                     WHEN @id1 = t.id1 AND @id2 = t.id2 THEN @rownum := @rownum + 1
                     ELSE @rownum := 1
                   END AS rk,
                   @id1 := t.id1,
                   @id2 := t.id2
              FROM temp3 t
              JOIN (SELECT @rownum := 0, @id1  := 0, @id2 := 0) r
          ORDER BY t.id1, t.id2, diff, RAND()) x
     WHERE x.rk = 1;

我正在使用查询在ID1-ID2对定义的每个组中随机绘制一行。我希望ID3与YEAR-MMDD的日期差异最小(即YEAR-MMDD和YEAR_ID3-MMDD_ID3之间的绝对差值应该最小化)。如果有多个具有完全相同的日期,则查询应随机选择一个。

如果这是表......

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

然后查询应返回

1,2,1991,0821,55 (OR 1,2,1991,0821,57 - ACCORDING TO THE RANDOM DRAW)
1,3,1990,0131,89

我在这里粘贴了一个TEST TABLE的SQL DUMP ......

DROP TABLE IF EXISTS `temp3`;
CREATE TABLE IF NOT EXISTS `temp3` (
  `id1` char(7) NOT NULL,
  `id2` char(7) NOT NULL,
  `YEAR` year(4) NOT NULL,
  `MMDD` char(4) NOT NULL,
  `id3` char(7) NOT NULL,
  `id3_YEAR` year(4) NOT NULL,
  `id3_MMDD` char(4) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


INSERT INTO `temp3` VALUES('1', '2', 1992, '0107', '55', 1991, '0528');
INSERT INTO `temp3` VALUES('1', '2', 1992, '0107', '57', 1991, '0701');
INSERT INTO `temp3` VALUES('1', '3', 1992, '0107', '88', 2000, '0101');
INSERT INTO `temp3` VALUES('1', '3', 1992, '0107', '44', 2000, '0101');

1 个答案:

答案 0 :(得分:1)

这是一个有效的解决方案。谢谢@OMG小马的帮助。

SELECT
    x.id1,
    x.id2,
    x.YEAR,
    x.MMDD,
    x.id3,
    x.id3_YEAR,
    x.id3_MMDD
FROM
(   SELECT
        t.*,
        @rownum := CASE 
            WHEN @id1 = t.id1 AND @id2 = t.id2 THEN @rownum + 1
            ELSE 1
            END AS rk,
        @id1 := t.id1,
        @id2 := t.id2
    FROM
    (   SELECT
            t.*,
             ABS(DATEDIFF(CONCAT(CAST(t.id3_YEAR AS CHAR(4)),'-', LEFT(t.id3_MMDD,2),'-',RIGHT(t.id3_MMDD,2)),
             CONCAT(CAST(t.YEAR AS CHAR(4)),'-', LEFT(t.MMDD,2),'-',RIGHT(t.MMDD,2)))) AS diff
        FROM temp3 t
        ORDER BY t.id1, t.id2, diff, RAND()
    ) t,
    (   SELECT @rownum := 0, @id1 := null, @id2 := null ) r
) x
WHERE x.rk = 1;