# Self Join Exercise. Have I over-complicated it?

This my 3rd question on the same exercise, but by no means a duplicate. The two previous questions were posted on StackOverflow here and here.

Now I'm posting my Oracle solution (below) that works. I wonder if the same could've been simpler and/or much more efficient. Not in terms of CTE or analytic expressions but with basic simple logic.

Data:

CREATE TABLE Readings (
user_id varchar(10),
x decimal(10,2),
y decimal(10,2)
);

INSERT ALL
SELECT * FROM dual;


Intermediate script:

SELECT r.user_id, rm.reading_time start_time, r.reading_time end_time,
(TO_CHAR(rm.x)||' ; '||TO_CHAR(rm.y)) start_point,
(TO_CHAR(r.x)||' ; '||TO_CHAR(r.y)) end_point,
SQRT(POWER(r.x-rm.x, 2)+POWER(r.x-rm.y, 2)) distance
JOIN Readings rm ON (r.user_id = rm.user_id and
ORDER BY 1,2;


Final script:

SELECT rr.user_id, SUM(rr.distance) "Total Distance",
SUM(rr.time_spent) "Total Time", SUM(rr.distance)/SUM(rr.time_spent) "Average Speed"
FROM
SQRT(POWER(r.x-rm.x, 2)+POWER(r.x-rm.y, 2)) distance
JOIN Readings rm ON (r.user_id = rm.user_id and
GROUP BY rr.user_id
ORDER BY 1;


Exercise Description

Multiple users roam a plain and at irregular time intervals report their coordinates (x, y). This information (user id, time-stamp and the coordinates) populate table Readings.

For each user that reported more than one set of coordinates we need to find total distance traveled, total time spent, and their average speed.

For the sake of simplicity coordinates are Cartesian and time-stamps are integers.

• Always put your units. Never reference columns numerically, always do it by name (this saves you from re-ordered columns in the select surprising you). Recent versions of Oracle support additional analysis functions that might help you (like LAG/LEAD) - see if any of those might help you. – Clockwork-Muse Mar 1 '13 at 22:00
• what are you actually trying to compute? What is the problem statement of the exercise? – abuzittin gillifirca Mar 2 '13 at 13:42
• @abuzittin gillifirca - added to the post (at the bottom) – PM 77-1 Mar 3 '13 at 0:53

• You have a bug. Change the row ('u3', 100, 500, 125) to ('u3', 99, 500, 125), and try again. You will see that u3 row disappears.

You can fix it with the following minimal change:

rm.reading_time = (SELECT MAX(r2.reading_time)
WHERE  r2.user_id=r.user_id -- you have to join on ids first

• There is another bug here: r.x-rm.y should have been r.y-rm.y.

• user_id, reading_time is a unique key by problem definition. Having it declared will enable us to detect some invalid inputs. What is the average speed of an object that can be at more than one place?

• I would go along with something like this for the intermediate script:

SELECT t.*, t2.*