Home All Groups Group Topic Archive Search About

Probably very trivial SQL Query question....

Author
18 Aug 2006 11:13 AM
Balt
Hi all,

I'm a bit of an SQL newbie and have the following problem:

I have two tables, one contains waypoints in latitude and longitude from a
trip.
The other contains trips.

so:

tabTrip: TripID, TripName
tabWaypoints: fk_TripID, Lat, Lon, tstamp

The relationship is such that one trip can have many waypoints.

Each waypoint has a timestamp (when it got added to the table)

How do I retrieve for each trip, that has waypoints not older than a certain
time, the most recently added waypoint's latitude and longitude as well as
the trip name (which is in the trip table) in one query?

Somehow those queries need to be worked into one, but how?

SELECT DISTINCT(fk_TripID) FROM tabWaypoint WHERE tstamp > '2006-08-18
20:00:00'

and

SELECT TOP(1) Lat, Lon FROM tabWaypoints WHERE tstamp > '2006-08-18
20:00:00' ORDER BY tstamp DESC

and

somehow, join the tabTrip so that I can select the TripName into this result
set...

Any help greatly appreciated, Thanks!

- Balt

Author
18 Aug 2006 12:24 PM
Damien
Balt wrote:
Show quote
> Hi all,
>
> I'm a bit of an SQL newbie and have the following problem:
>
> I have two tables, one contains waypoints in latitude and longitude from a
> trip.
> The other contains trips.
>
> so:
>
> tabTrip: TripID, TripName
> tabWaypoints: fk_TripID, Lat, Lon, tstamp
>
> The relationship is such that one trip can have many waypoints.
>
> Each waypoint has a timestamp (when it got added to the table)
>
> How do I retrieve for each trip, that has waypoints not older than a certain
> time, the most recently added waypoint's latitude and longitude as well as
> the trip name (which is in the trip table) in one query?
>
> Somehow those queries need to be worked into one, but how?
>
> SELECT DISTINCT(fk_TripID) FROM tabWaypoint WHERE tstamp > '2006-08-18
> 20:00:00'
>
> and
>
> SELECT TOP(1) Lat, Lon FROM tabWaypoints WHERE tstamp > '2006-08-18
> 20:00:00' ORDER BY tstamp DESC
>
> and
>
> somehow, join the tabTrip so that I can select the TripName into this result
> set...
>
> Any help greatly appreciated, Thanks!
>
> - Balt

select t.TripName, tw.Lat, tw.Lon, tw.tstamp
from
tabTrip t inner join tabWaypoints tw on t.TripID = tw.TripID left join
tabWaypoints later on t.TripID = later.TripID and later.tstamp >
tw.tstamp
where
tw.tstamp > '2006-08-08T20:00:00'

will that do you?

Damien
Author
18 Aug 2006 10:28 PM
Balt
Damien,

your version retrieves an infinite amount of result sets... Stopped it after
500'000 records (but there are only 10'000 waypoints...)

Cheers

-Balt

Show quote
"Damien" wrote:

> Balt wrote:
> > Hi all,
> >
> > I'm a bit of an SQL newbie and have the following problem:
> >
> > I have two tables, one contains waypoints in latitude and longitude from a
> > trip.
> > The other contains trips.
> >
> > so:
> >
> > tabTrip: TripID, TripName
> > tabWaypoints: fk_TripID, Lat, Lon, tstamp
> >
> > The relationship is such that one trip can have many waypoints.
> >
> > Each waypoint has a timestamp (when it got added to the table)
> >
> > How do I retrieve for each trip, that has waypoints not older than a certain
> > time, the most recently added waypoint's latitude and longitude as well as
> > the trip name (which is in the trip table) in one query?
> >
> > Somehow those queries need to be worked into one, but how?
> >
> > SELECT DISTINCT(fk_TripID) FROM tabWaypoint WHERE tstamp > '2006-08-18
> > 20:00:00'
> >
> > and
> >
> > SELECT TOP(1) Lat, Lon FROM tabWaypoints WHERE tstamp > '2006-08-18
> > 20:00:00' ORDER BY tstamp DESC
> >
> > and
> >
> > somehow, join the tabTrip so that I can select the TripName into this result
> > set...
> >
> > Any help greatly appreciated, Thanks!
> >
> > - Balt
>
> select t.TripName, tw.Lat, tw.Lon, tw.tstamp
> from
> tabTrip t inner join tabWaypoints tw on t.TripID = tw.TripID left join
> tabWaypoints later on t.TripID = later.TripID and later.tstamp >
> tw.tstamp
> where
> tw.tstamp > '2006-08-08T20:00:00'
>
> will that do you?
>
> Damien
>
>
Author
21 Aug 2006 8:00 AM
Damien
Balt wrote:
Show quote
> "Damien" wrote:
>
> > Balt wrote:
> > > Hi all,
> > >
> > > I'm a bit of an SQL newbie and have the following problem:
> > >
> > > I have two tables, one contains waypoints in latitude and longitude from a
> > > trip.
> > > The other contains trips.
> > >
> > > so:
> > >
> > > tabTrip: TripID, TripName
> > > tabWaypoints: fk_TripID, Lat, Lon, tstamp
> > >
> > > The relationship is such that one trip can have many waypoints.
> > >
> > > Each waypoint has a timestamp (when it got added to the table)
> > >
> > > How do I retrieve for each trip, that has waypoints not older than a certain
> > > time, the most recently added waypoint's latitude and longitude as well as
> > > the trip name (which is in the trip table) in one query?
> > >
> > > Somehow those queries need to be worked into one, but how?
> > >
> > > SELECT DISTINCT(fk_TripID) FROM tabWaypoint WHERE tstamp > '2006-08-18
> > > 20:00:00'
> > >
> > > and
> > >
> > > SELECT TOP(1) Lat, Lon FROM tabWaypoints WHERE tstamp > '2006-08-18
> > > 20:00:00' ORDER BY tstamp DESC
> > >
> > > and
> > >
> > > somehow, join the tabTrip so that I can select the TripName into this result
> > > set...
> > >
> > > Any help greatly appreciated, Thanks!
> > >
> > > - Balt
> >
> > select t.TripName, tw.Lat, tw.Lon, tw.tstamp
> > from
> > tabTrip t inner join tabWaypoints tw on t.TripID = tw.TripID left join
> > tabWaypoints later on t.TripID = later.TripID and later.tstamp >
> > tw.tstamp
> > where
> > tw.tstamp > '2006-08-08T20:00:00'
> >
> > will that do you?
> >
> > Damien
> >
> >
> Damien,
>
> your version retrieves an infinite amount of result sets... Stopped it after
> 500'000 records (but there are only 10'000 waypoints...)
>
> Cheers
>
> -Balt
>

Doh. As I was writing it, I kept saying to myself "remember to check
for null in the where clause, remember to check for null in the where
clause...", which inevitably means I forgot to include "and
later.TripID is null" in the where clause.

Damien
Author
18 Aug 2006 12:25 PM
Roy Harvey
This should give you something to work with.

SELECT T.*, W.Lat, W.Long, W.tstamp
  FROM tabTrip as T
  JOIN tabWaypoints as W
    ON T.TripID = W.fw_TripID
WHERE W.tstamp =
       (select max(tstamp) from tabWaypoints as M
         where W.tstamp = M.tstamp)
   AND W.tstamp > '2006-08-18 20:00:00'

Roy Harvey
Beacon Falls, CT

On Fri, 18 Aug 2006 04:13:01 -0700, Balt
<B***@discussions.microsoft.com> wrote:

Show quote
>Hi all,
>
>I'm a bit of an SQL newbie and have the following problem:
>
>I have two tables, one contains waypoints in latitude and longitude from a
>trip.
>The other contains trips.
>
>so:
>
>tabTrip: TripID, TripName
>tabWaypoints: fk_TripID, Lat, Lon, tstamp
>
>The relationship is such that one trip can have many waypoints.
>
>Each waypoint has a timestamp (when it got added to the table)
>
>How do I retrieve for each trip, that has waypoints not older than a certain
>time, the most recently added waypoint's latitude and longitude as well as
>the trip name (which is in the trip table) in one query?
>
>Somehow those queries need to be worked into one, but how?
>
>SELECT DISTINCT(fk_TripID) FROM tabWaypoint WHERE tstamp > '2006-08-18
>20:00:00'
>
>and
>
>SELECT TOP(1) Lat, Lon FROM tabWaypoints WHERE tstamp > '2006-08-18
>20:00:00' ORDER BY tstamp DESC
>
>and
>
>somehow, join the tabTrip so that I can select the TripName into this result
>set...
>
>Any help greatly appreciated, Thanks!
>
>- Balt
>
Author
18 Aug 2006 10:25 PM
Balt
Hi Roy,

thanks, this almost works. It does return non distinct trip id's. I only
need the most recent ONE position for each trip. Your query returns all of
them.

Cheers

- Balt

Show quote
"Roy Harvey" wrote:

> This should give you something to work with.
>
> SELECT T.*, W.Lat, W.Long, W.tstamp
>   FROM tabTrip as T
>   JOIN tabWaypoints as W
>     ON T.TripID = W.fw_TripID
>  WHERE W.tstamp =
>        (select max(tstamp) from tabWaypoints as M
>          where W.tstamp = M.tstamp)
>    AND W.tstamp > '2006-08-18 20:00:00'
>
> Roy Harvey
> Beacon Falls, CT
>
> On Fri, 18 Aug 2006 04:13:01 -0700, Balt
> <B***@discussions.microsoft.com> wrote:
>
> >Hi all,
> >
> >I'm a bit of an SQL newbie and have the following problem:
> >
> >I have two tables, one contains waypoints in latitude and longitude from a
> >trip.
> >The other contains trips.
> >
> >so:
> >
> >tabTrip: TripID, TripName
> >tabWaypoints: fk_TripID, Lat, Lon, tstamp
> >
> >The relationship is such that one trip can have many waypoints.
> >
> >Each waypoint has a timestamp (when it got added to the table)
> >
> >How do I retrieve for each trip, that has waypoints not older than a certain
> >time, the most recently added waypoint's latitude and longitude as well as
> >the trip name (which is in the trip table) in one query?
> >
> >Somehow those queries need to be worked into one, but how?
> >
> >SELECT DISTINCT(fk_TripID) FROM tabWaypoint WHERE tstamp > '2006-08-18
> >20:00:00'
> >
> >and
> >
> >SELECT TOP(1) Lat, Lon FROM tabWaypoints WHERE tstamp > '2006-08-18
> >20:00:00' ORDER BY tstamp DESC
> >
> >and
> >
> >somehow, join the tabTrip so that I can select the TripName into this result
> >set...
> >
> >Any help greatly appreciated, Thanks!
> >
> >- Balt
> >
>
Author
19 Aug 2006 1:07 AM
Roy Harvey
On Fri, 18 Aug 2006 15:25:02 -0700, Balt
<B***@discussions.microsoft.com> wrote:

>Hi Roy,
>
>thanks, this almost works. It does return non distinct trip id's. I only
>need the most recent ONE position for each trip. Your query returns all of
>them.
>
>Cheers
>
>- Balt

That's because I screwed up the subquer, I think.  The corellation was
supposed to be on fw_TripID.

Maybe I did better this time:

SELECT T.*, W.Lat, W.Long, W.tstamp
  FROM tabTrip as T
  JOIN tabWaypoints as W
    ON T.TripID = W.fw_TripID
WHERE W.tstamp =
       (select max(tstamp) from tabWaypoints as M
         where W.fw_TripID = M.fw_TripID)
   AND W.tstamp > '2006-08-18 20:00:00'

Roy
Author
19 Aug 2006 4:43 AM
Balt
That was it, thanks!

Guess I'll have to learn using sub-queries!

Cheers

- Balt

Show quote
"Roy Harvey" wrote:

> On Fri, 18 Aug 2006 15:25:02 -0700, Balt
> <B***@discussions.microsoft.com> wrote:
>
> >Hi Roy,
> >
> >thanks, this almost works. It does return non distinct trip id's. I only
> >need the most recent ONE position for each trip. Your query returns all of
> >them.
> >
> >Cheers
> >
> >- Balt
>
> That's because I screwed up the subquer, I think.  The corellation was
> supposed to be on fw_TripID.
>
> Maybe I did better this time:
>
> SELECT T.*, W.Lat, W.Long, W.tstamp
>   FROM tabTrip as T
>   JOIN tabWaypoints as W
>     ON T.TripID = W.fw_TripID
>  WHERE W.tstamp =
>        (select max(tstamp) from tabWaypoints as M
>          where W.fw_TripID = M.fw_TripID)
>    AND W.tstamp > '2006-08-18 20:00:00'
>
> Roy
>

AddThis Social Bookmark Button