GROUP BY with MAX (DATE) [duplicate]
To pytanie ma już odpowiedź tutaj:
Próbuję wymienić ostatni cel podróży (maksymalny czas odjazdu) dla każdego pociągu w tabeli, na przykład :
Train Dest Time
1 HK 10:00
1 SH 12:00
1 SZ 14:00
2 HK 13:00
2 SH 09:00
2 SZ 07:00
Pożądany rezultat powinien być:
Train Dest Time
1 SZ 14:00
2 HK 13:00
Próbowałem użyć
SELECT Train, Dest, MAX(Time)
FROM TrainTable
GROUP BY Train
By I got a "ora-00979 not a GROUP BY expression" błąd mówiący, że muszę dołączyć "Dest" do mojej grupy według oświadczenia. Ale na pewno nie tego chcę...
Czy można to zrobić w jednej linii SQL?
6 answers
Nie można dołączać niezagregowanych kolumn do zestawu wyników, które nie są pogrupowane. Jeśli pociąg ma tylko jeden cel podróży, po prostu dodaj kolumnę cel do klauzuli group by, w przeciwnym razie musisz przemyśleć swoje zapytanie.
Try:
SELECT t.Train, t.Dest, r.MaxTime
FROM (
SELECT Train, MAX(Time) as MaxTime
FROM TrainTable
GROUP BY Train
) r
INNER JOIN TrainTable t
ON t.Train = r.Train AND t.Time = r.MaxTime
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-30 06:23:36
SELECT train, dest, time FROM (
SELECT train, dest, time,
RANK() OVER (PARTITION BY train ORDER BY time DESC) dest_rank
FROM traintable
) where dest_rank = 1
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-16 07:42:11
Oto przykład, który używa tylko lewego połączenia i uważam, że jest bardziej efektywny niż jakakolwiek grupa według metody tam: ExchangeCore Blog
SELECT t1.*
FROM TrainTable t1 LEFT JOIN TrainTable t2
ON (t1.Train = t2.Train AND t1.Time < t2.Time)
WHERE t2.Time IS NULL;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-12 11:43:47
Inne rozwiązanie:
select * from traintable
where (train, time) in (select train, max(time) from traintable group by train);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-23 16:07:31
Tak długo, jak nie ma duplikatów (a pociągi zazwyczaj przyjeżdżają tylko na jedną stację na raz)...
select Train, MAX(Time),
max(Dest) keep (DENSE_RANK LAST ORDER BY Time) max_keep
from TrainTable
GROUP BY Train;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-16 23:24:39
Wiem, że jestem spóźniona na imprezę, ale spróbuj tego...
SELECT
`Train`,
`Dest`,
SUBSTRING_INDEX(GROUP_CONCAT(`Time` ORDER BY `Time` DESC), ",", 1) AS `Time`
FROM TrainTable
GROUP BY Train;
Src: Group Concat Documentation
Edit: fixed SQL syntax
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-14 13:29:16