【发布时间】: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 列,自动增量是我的主键