Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL]Jakie klauzule wykorzystać do rozwiązania tego zadania?
Forum PHP.pl > Forum > Bazy danych > MySQL
szczrzcz
Dostałem zadanie na rozmowie na którym poległem, i które na spokojnie w domu też nie wiem jak ugryźć. Proszę o wskazówki jak można to rozwiązać.
Zacząłem od napisania
Kod
SELECT event_type, sum(value) as value FROM `events` GROUP BY event_type
co oczywiście jest dalekie od rozwiązania.



Given a table events with the following structure:
  1. CREATE TABLE events (
  2. event_type integer NOT NULL,
  3. value integer NOT NULL,
  4. time timestamp NOT NULL,
  5. UNIQUE(event_type, time)
  6. );

write an SQL query that, for each event_type that has been registered more than once, returns the difference between the latest (i.e. the most recent in
terms of time) and the second latest value. The table should be ordered by event_type (in ascending order).
For example, given the following data:

Kod
event_type | value | time
------------+------------+--------------------
2              | 5        | 2015-05-09 12:42:00
4              | -42     | 2015-05-09 13:19:57
2              | 2        | 2015-05-09 14:48:30
2              | 7        | 2015-05-09 12:54:39
3              | 16      | 2015-05-09 13:19:57
3              | 20      | 2015-05-09 15:01:09


your query should return the following rowset:

Kod
event_type | value
------------+-----------
2              | -5
3              | 4

For the event_type 2, the latest value is 2 and the second latest value is 7, so the difference between them is −5.
nospor
Przenosze bo problem zadania to nie jest przedszkole.

ps: na jaka pozycje startowales?
szczrzcz
Full Stack / PHP Developer.
Salary (without VAT) ?8000 - 12000 PLN / Month.
REQUIREMENTS: SQL 3/3 gwiazdki

aplikując zaznaczyłem, że startuję na młodszego, ja się czuję bardzo młodo.

ps znalazłem na stacku rozwiązanie https://stackoverflow.com/questions/3176123...ond-latest-term w życiu bym tego nie zrobił
nospor
sql 3/3 to powiedzmy ze mogli chciec wymagac. Nie mniej jednak czasami odnosze wrazenie ze tego typu zapytania to tylko sztuka dla sztuki. Na studiach meczono nas takim czyms a potem w praktyce czlowiek nigdy tego nie uzywal bo ani to optymalne ani przyjazne dla srodowiska...
szczrzcz
A to akurat winić trzeba codility.com a nie tego konkretnego pracodawcę. Zadanie jest stamtąd.
mmmmmmm
http://sqlfiddle.com/#!9/f9357f/6
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.