【问题标题】:Query to find result with max rundate with max horizon查询以查找具有最大运行日期和最大地平线的结果
【发布时间】:2014-06-23 06:53:57
【问题描述】:

表格数据如下:

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 

我想为每个事件和市场组选择最大运行日期,然后在该组中选择最大范围,然后打印整行。

想要的结果是:

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
);

它为最大水平选择多个运行日期。

【问题讨论】:

  • 您的示例显示了事件和 mpid 之间的一对一关系。这是正确的吗?另外,你的 PRIMARY KEY 是什么?
  • 不,我已经更新了示例。我有一个 ID 列,自动增量是我的主键

标签: mysql sql max


【解决方案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

【讨论】:

  • 我也在修改我现有的查询。我也想出了类似的解决方案。在我看来很好 。谢谢!
  • @Ajay 很高兴为您提供帮助
  • 我认为这个解决方案是有缺陷的。如果再添加一行 (1,1,'2014-06-21',365,'bar') 会怎样?
【解决方案2】:

正确的方法...

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;

【讨论】:

    【解决方案3】:

    您可以按事件和市场分组并获取 MAX(运行日期)。可以关注的MAX(horizo​​n)。

    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
    

    【讨论】:

    • 我没有得到你能写完整的查询。我正在使用 mysql
    • SELECT event_id, marketplaceid, MAX(rundate) AS rundate, substring_index( group_concate(horizon ORDER BY rundate DESC, horizo​​n DESC), ',', 1 ) AS Horizo​​n FROM dsie_result_overalls GROUP BY event_id, marketid
    • 我不知道你会用otherData做什么,我在你的表结构中找不到id
    • 好主意,但请注意,如果不进行一些调整,这将获取错误的 id(id PK 是作为对原始问题的后期修改添加的)
    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多