查询以使用max rundate查找具有最大范围的结果

时间:2014-06-23 06:53:58

标签: mysql sql max

表格数据如下:

EventID | MPID | rundate     | Horizon | otherData 
1       | 1    | 23-Jun-2014 | 360     | other value
1       | 1    | 23-Jun-2014 | 365     | pther value 
1       | 1    | 23-Jun-2014 | 300     | pther value 
1       | 1    | 22-Jun-2014 | 700     | pther value 
1       | 2    | 23-Jun-2014 | 400     | other value
1       | 2    | 23-Jun-2014 | 340     | oth
2       | 3    | 23-Jun-2014 | 360     | pther value 
2       | 3    | 23-Jun-2014 | 300     | pther value 
2       | 3    | 22-Jun-2014 | 365     | pther value 

我想为每个事件和市场组选择max rundate,然后在该组中选择max horizo​​n,然后打印整行。

期望的结果是:

EventID | MPID | rundate     | Horizon | otherData 
1       | 1    | 23-Jun-2014 | 365     | pther value 
1       | 2    | 23-Jun-2014 | 400     | other value
2       | 3    | 23-Jun-2014 | 360     | pther value

请告诉我这方面的SQL查询。

我尝试了以下查询,但它不起作用:

SELECT * from dsie_result_overalls where id in (
SELECT k.id from dsie_result_overalls k,
(
SELECT a.event_id, a.marketplaceid, MAX(a.horizon) as horizon FROM dsie_result_overalls a,
(
    SELECT id, event_id, marketplaceid, MAX(rundate) AS rundate FROM dsie_result_overalls
    GROUP BY event_id, marketplaceid
) b
WHERE a.event_id = b.event_id AND a.marketplaceid = b.marketplaceid AND a.rundate = b.rundate
GROUP BY a.event_id, a.marketplaceid
    ) l WHERE k.event_id = l.event_id AND k.marketplaceid = l.marketplaceid AND k.horizon  =    l.horizon
);

它为最大范围选择多个rundate。

3 个答案:

答案 0 :(得分:1)

尝试此查询

Select T.* From Tbl T JOIN
   ( Select Max(S.Horizon) MaxHorizon,Max(S.rundate) As dte,S.EventID,S.MPID
     From Tbl S Join
        ( Select T1.EventID,Max(T1.rundate) As Maxrundate,T1.MPID
          From Tbl T1 Group By T1.EventID,T1.MPID
        ) JR On S.rundate = JR.Maxrundate AND S.EventID = JR.EventID AND S.MPID = JR.MPID
        Group By S.MPID,S.EventID
   )R ON T.Horizon = R.MaxHorizon AND T.EventID = R.EventID AND T.MPID = R.MPID AND T.rundate = R.dte 

<强> Fiddle Demo


输出


EventID | MPID | rundate     | Horizon | otherData 
1       | 1    | 23-Jun-2014 | 365     | pther value 
1       | 2    | 23-Jun-2014 | 400     | other value
2       | 3    | 23-Jun-2014 | 360     | pther value

答案 1 :(得分:0)

您可以按事件和市场分组获得MAX(rundate)。你可以跟随MAX(地平线)。

SELECT eventid
     , mpid
     , MAX(rundate) rundate
     , SUBSTRING_INDEX(GROUP_CONCAT(horizon ORDER BY rundate DESC, horizon DESC),',',1) horizon 
  FROM dsie_result_overalls 
 GROUP  
    BY eventid
     , mpid

答案 2 :(得分:0)

正确的方式......

SELECT x.*
  FROM dsie_result_overalls x
  JOIN
     ( SELECT a.eventid 
            , a.mpid
            , a.rundate
            , MAX(a.horizon) max_horizon
         FROM dsie_result_overalls a
         JOIN
            ( SELECT eventid
                   , mpid
                   , MAX(rundate) max_rundate
                FROM dsie_result_overalls
               GROUP
                  BY eventid
                   , mpid
            ) b
           ON b.eventid = a.eventid
          AND b.mpid = a.mpid
          AND b.max_rundate = a.rundate
        GROUP
           BY a.eventid
            , a.mpid
            , a.rundate
     ) y
    ON y.eventid = x.eventid
   AND y.mpid = x.mpid
   AND y.rundate = x.rundate
   AND y.max_horizon = x.horizon;

黑客的方式......

SELECT *  
  FROM 
     ( SELECT * 
         FROM dsie_result_overalls 
        ORDER 
           BY eventid
            , mpid
            , rundate DESC
            , horizon DESC
     ) x 
 GROUP 
    BY eventid
     , mpid;

老式的方式......

SELECT x.* 
  FROM dsie_result_overalls x
  LEFT 
  JOIN dsie_result_overalls y 
    ON y.eventid = x.eventid 
   AND y.mpid = x.mpid 
   AND (y.rundate > x.rundate OR (y.rundate = x.rundate AND y.horizon > x.horizon)) 
 WHERE y.id IS NULL;