简体   繁体   English

寻找SQL GROUP BY…WHERE MIN日期的解决方案

[英]Looking for a solution to a SQL GROUP BY … WHERE MIN date

EDIT: The following question is angled at both MS-SQL and MySQL. 编辑:以下问题针对MS-SQL和MySQL。

I've been pondering over this for a good 7 hours now. 我已经考虑了7个多小时了。 I've seen many stack overflow answers that are similar, but none that i've properly understood or worked out how to implement. 我已经看到许多类似的堆栈溢出答案,但是我都没有正确理解或解决如何实现的答案。

I am looking to SELECT id, title, etc etc FROM a table, WHERE the date is the next available date AFTER NOW(). 我想从表中选择ID,标题等,其中日期是AFTER NOW()之后的下一个可用日期。 The catch is, it needs to be GROUPED BY one particular column. 要注意的是,它需要按一个特定的列进行分组。

Here is the table: 表格如下:

================================== ==================================

id    |    name    |    date_start    |    sequence_id
--------------------------------------------------------
  1    |   Foo1     |     20150520     |       70
  2    |   Foo2     |     20150521     |       70
  3    |   Foo3     |     20150522     |       70
  4    |   Foo4     |     20150523     |       70
  5    |   FooX     |     20150524     |       70
  6    |   FooY     |     20150525     |       70
  7    |   Bar      |     20150821     |       61
  8    |   BarN     |     20151110     |       43
  9    |   BarZ     |     20151104     |       43

And here is what I would like to see: 这是我想看到的:

================================== ==================================

id    |    name    |    date_start    |    sequence_id
--------------------------------------------------------
  1    |   Foo1     |     20150520     |       70
  7    |   Bar      |     20150821     |       61
  9    |   BarZ     |     20151104     |       43

The results are filtered by MIN(date_start) > NOW() AND GROUPED BY sequence_id. 结果通过MIN(date_start)> NOW()和GROUPED BY sequence_id进行过滤。

I'm not entirely sure this can be achieved with a GROUP BY as the rest of the columns would need to be contained within an aggregate function which I don't think will work. 我不完全确定可以使用GROUP BY实现此功能,因为其余列将需要包含在我认为不起作用的聚合函数中。

Does anyone have an answer for this dilemma? 有人对此困境有答案吗?

Many Thanks! 非常感谢!

Simon 西蒙

Just use a join and aggregation in a subquery: 只需在子查询中使用join和聚合:

select t.*
from table t join
     (select sequence_id, min(date_start) as minds
      from table t
      group by sequence_id
     ) tt
     on tt.sequence_id = t.sequence_id and t.date_start = tt.minds;

This is standard SQL, so it should run in any database. 这是标准的SQL,因此它应在任何数据库中运行。

http://sqlfiddle.com/#!9/d8576/4 http://sqlfiddle.com/#!9/d8576/4

SELECT *
FROM table1 as t1
LEFT JOIN
  (SELECT * 
   FROM table1
   WHERE date_start>NOW()
  ) as t2
ON t1.sequence_id = t2.sequence_id and t1.date_start>t2.date_start
WHERE t1.date_start>NOW() and t2.date_start IS NULL
GROUP BY t1.sequence_id

MSSQL fiddle MSSQL小提琴

SELECT *
FROM table1 as t1
LEFT JOIN
  (SELECT * 
   FROM table1
   WHERE date_start>GetDate()
  ) as t2
ON t1.sequence_id = t2.sequence_id and t1.date_start>t2.date_start
WHERE t1.date_start>GetDate() and t2.date_start IS NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM