Re: sql query question ?

From: Trilok Kumar <trilokumar(at)yahoo(dot)co(dot)uk>
To: Shane Ambler <pgsql(at)Sheeky(dot)Biz>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: sql query question ?
Date: 2007-12-31 13:53:18
Message-ID: 393278.84548.qm@web27203.mail.ukl.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Dear Shane,

Thanks for the reply and your observation about the
word i have used. It is idle odometer reading.

The actual Scenario is that the vehicle is taken by
the driver.

When he comes the next day. He is suppose to login
again.
Here i am trying to find out how much distance has the
vehicle travelled before it login again,give a date
range and the vehicle id.

The below query would give me the total odometer
reading during a single trip.

select vehicle_master_id,
(ending_odometer - starting_odometer) as
unmetered_travel
from vehicle_duty_cycle_summary;

Thanks

Trilok


--- Shane Ambler <pgsql(at)Sheeky(dot)Biz> wrote:

> Trilok Kumar wrote:
> > Hi All,
> >
> > I have a table called
> >
> > vehicle_duty_cycle_summary
> >
> >
> > vehicle_master_id | starting_odometer |
> > ending_odometer | login_time |
>
> > logout_time
> >
>
-------------------+-------------------+-----------------+----------------------------+----------------------------
> >
> > 4 | 53379.00 | 53504.00 |
> 2006-12-19
> > 16:19:16.584547 | 2006-12-20 07:12:57.716907
>
> >
> > I would like to compute the following on this
> table.
> >
> > Idle time of vehicel=(ending_odometer reading of
> the
> > previous day -
> > starting_odometer reading of the present day) for
> > every vehicle
>
> I would think your naming may be confusing and may
> not be
> implemented(recorded?) very well.
>
> I think Idle Time is a misleading name by your
> explanation - Idle time
> would be defined as (logout_time - previous
> login_time) which gives you
> the time the vehicle was sitting in the garage.
>
> What you want may be better called unmetered_travel
> and would be the
> distance traveled between login_time and logout_time
> This would simply be
> select vehicle_master_id,
> (ending_odometer - starting_odometer) as
> unmetered_travel
> from vehicle_duty_cycle_summary;
>
> Going by the naming you have used it would appear
> that you are recording
> the time spent in the garage (going by the data you
> have shown I would
> say this is a company car garage not a repair shop)
>
> One record would appear to record the time the car
> is in the garage -
> login_time would be the time the employee returned
> the car and
> logout_time would be when the car next went out to
> someone.
> I would think you want the opposite of that - the
> time and odometer
> reading when an employee takes the car and the time
> and odometer of when
> it is returned and the employee_id of who had it.
> This will give you who
> used the car at what time and what distances they
> travelled (which of
> course would be work related travel)
>
> Going with those changes -
>
> The distance traveled by an employee is easy to
> workout, if you wanted
> to workout the unmetered (non-work) distance
> traveled you could try
> something like (untested) -
>
> select
> v1.vehicle_master_id
> , v1.starting_odometer -
> (select v2.ending_odometer
> from vehicle_duty_cycle_summary v2
>
> where v2.vehicle_master_id = v1.vehicle_master_id
> and v2.login_time < v1.logout_time
>
> order by v2.login_time desc limit 1)
> as unmetered_travel
>
> from vehicle_duty_cycle_summary v1
>
> where v1.vehicle_master_id = 4;
>
>
> I would calculate idle time as -
>
> select
> v1.vehicle_master_id
> , v1.logout_time -
> (select v2.login_time
> from vehicle_duty_cycle_summary v2
>
> where v2.vehicle_master_id = v1.vehicle_master_id
> and v2.login_time < v1.logout_time
>
> order by v2.login_time desc limit 1)
> as unmetered_travel
>
> from vehicle_duty_cycle_summary v1
>
> where v1.vehicle_master_id = 4;
>
>
> If this isn't the way it should work you should be
> able to adapt the
> query to match your definition of idle time.
>
>
> --
>
> Shane Ambler
> pgSQL (at) Sheeky (dot) Biz
>
> Get Sheeky @ http://Sheeky.Biz
>

___________________________________________________________
Yahoo! Answers - Got a question? Someone out there knows the answer. Try it
now.
http://uk.answers.yahoo.com/

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh 2007-12-31 18:52:03 Limit # of recs on inner join?
Previous Message Pavel Stehule 2007-12-30 08:44:46 Re: temp table existence