如何从每个表中提取每天的FIRST'StartAddress'。
结果必须如下所示。
+------------+---------------+--------------------+--------------------+
| Date | SamplingStart | StartAddress | StopAddress |
+------------+---------------+--------------------+--------------------+
| 2016-02-22 | 08:00:00 | 26 Riverside Drive | 70 Piet Retief |
| 2016-02-23 | 05:15:00 | Balizza | Mars |
+------------+---------------+--------------------+--------------------|
表:
+------------+---------------+--------------------+--------------------+
| Date | SamplingStart | StartAddress | StopAddress |
+------------+---------------+--------------------+--------------------+
| 2016-02-22 | 08:00:00 | 26 Riverside Drive | 70 Piet Retief |
| 2016-02-22 | 09:00:00 | 10 Glen Drive | 16 Olga Street |
| 2016-02-22 | 10:00:00 | 66 Bremerhof | 26 Bluewater Drive |
| 2016-02-22 | 11:00:00 | 7 New Street | Walker Drive |
| 2016-02-22 | 13:00:00 | Goodwood Street | Blouberg |
| 2016-02-22 | 15:00:00 | Marine Drive | William Moffet |
| 2016-02-22 | 18:00:00 | Jumanji | Paterson |
| 2016-02-23 | 07:00:00 | 25 Eazi Street | 70 Kings Street |
| 2016-02-23 | 07:20:00 | Cool Blue | Pick n Pay |
| 2016-02-23 | 05:15:00 | Balizza | Mars |
| 2016-02-23 | 20:15:00 | Neverland | Club Shakes |
| 2016-02-23 | 23:15:00 | Castros | Stones |
| 2016-02-23 | 14:45:00 | Seagate | Ermelo |
| 2016-02-23 | 16:30:00 | Mafikeng | Goodwood |
+------------+---------------+--------------------+--------------------+
答案 0 :(得分:2)
试试这个:
SELECT
*
FROM
tbl
WHERE
(Date, SamplingStart) IN
(
SELECT tbl2.Date, MIN(tbl2.SamplingStart)
FROM tbl tbl2
WHERE tbl.Date = tbl2.Date
);
答案 1 :(得分:1)
您可以通过为每个日期添加行号来实现此目的。
<强>查询强>
select t.`Date`, t.SamplingStart, t.StartAddress, t.StopAddress from
(
select `Date`,SamplingStart, StartAddress, StopAddress,
(
case `Date` when @curA
then @curRow := @curRow + 1
else @curRow := 1 and @curA := `Date` end
) as rn
from your_table_name t,
(select @curRow := 0, @curA := '') r
order by `Date`, SamplingStart
)t
where t.rn = 1;
答案 2 :(得分:1)
您可以使用NOT EXISTS()
SELECT t.*
FROM YourTable t
WHERE NOT EXISTS(SELECT 1 FROM YourTable s
WHERE t.date = s.date
and t.SamplingStart > s.SamplingStart)
答案 3 :(得分:1)
Select Date, MIN(SamplingStart) AS SamplingStart, StartAddress, StopAddress
from Table Group By Date;