Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id EA1E32E30A8 for ; Mon, 31 Dec 2007 09:53:28 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 23137-08 for ; Mon, 31 Dec 2007 09:53:15 -0400 (AST) Received: from web27203.mail.ukl.yahoo.com (web27203.mail.ukl.yahoo.com [217.146.182.93]) by postgresql.org (Postfix) with SMTP id 4536C2E30A7 for ; Mon, 31 Dec 2007 09:53:20 -0400 (AST) Received: (qmail 24486 invoked by uid 60001); 31 Dec 2007 13:53:18 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.co.uk; h=X-YMail-OSG:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=UuQ/h+p45g7fGEz3R06jTnoVXkvlnsCTsKkACccx/OD9HMGM9jB4Mm7GFUmnCavKHpx3k89DS6R/PaZWURbUtri4py4NAF9MuIDof7CGHzVKJFgsVfoY3NLBwyd1T11q7mJR0QEPlNSEiRJTQ+n6oaAOygxNIVkfo/3FgcjXfeE=; X-YMail-OSG: z5bO51wVM1lUtcmRnFEHm5qnCQP5Uw9N41C8z1yUHw44ysgWhZ1WMTR8dgf._EkTmR_BDFLaHFYXpPDTbGArfxyBE_nlAfVIsISDbCbx6jiXX4chzK85jhJ5qgU- Received: from [59.92.208.177] by web27203.mail.ukl.yahoo.com via HTTP; Mon, 31 Dec 2007 13:53:18 GMT Date: Mon, 31 Dec 2007 13:53:18 +0000 (GMT) From: Trilok Kumar Subject: Re: sql query question ? To: Shane Ambler Cc: pgsql-sql@postgresql.org In-Reply-To: <4776823D.30707@Sheeky.Biz> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <393278.84548.qm@web27203.mail.ukl.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.002 tagged_above=0 required=5 tests=AWL=1.002, BAYES_50=0.001 X-Spam-Level: * X-Archive-Number: 200712/160 X-Sequence-Number: 29852 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 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/