想象一下如下设置的无序表:
+----+------------+----------+-----------+
| ID | Project | Resource | StartDate |
+----+------------+----------+-----------+
| 1 | ExtraParts | Mark | 24/01 |
| 2 | ExtraParts | Sam | 22/01 |
| 3 | TimeLabour | Sally | 19/01 |
| 4 | TimeLabour | Sarena | 23/01 |
| 5 | Runway | Olly | 14/02 |
| 6 | Runway | Mary | 14/05 |
+----+------------+----------+-----------+
我想通过最早的StartDate为每个项目订购,但仍然将每个项目的资源组合在一起(不确定我是否解释了这个权利,但下面是我想要实现的目标)
+----+-------------+-----------+-----------+
| ID | Project | Resource | StartDate |
+----+-------------+-----------+-----------+
| 1 | TimeLabor | Sally | 19/01 |
| 2 | TimeLabor | Sarena | 23/01 |
| 3 | ExtraParts | Sam | 22/01 |
| 4 | ExtraParts | Mark | 24/01 |
| 5 | Runway | Olly | 14/02 |
| 6 | Runway | Mary | 14/05 |
+----+-------------+-----------+-----------+
如果我按照StartDate,Project进行ORDER,那么结果将会混淆项目。如果我是ORDER BY项目,StartDate然后结果将按项目的字母顺序排序,然后在同一项目中按日期排序(如果这是有意义的)。如上所述,我想按最早的StartDate为每个项目订购,同时仍然将项目组合在一起(不是聚合分组,只是一个在另一个之下)。
非常感谢任何帮助! :)
答案 0 :(得分:0)
SELECT project, resource, startdate
FROM testing
ORDER BY project, STR_TO_DATE(StartDate, '%d/%m')
基本上你的表将日期存储为字符串,为了让你订购,你需要将字符串转换为识别的日期......所以STR_TO_DATE()是一种方法:)
答案 1 :(得分:0)
您需要计算订购日期:
select t.*
from table t join
(select project, min(startdate) as minsd
from table t
group by project
) tp
on t.project = tp.project
order by tp.minsd, t.project;
注意:这假设startdate
确实存储为日期而不是字符串。如果它存储为字符串,则应首先将其转换为日期。