Skip site navigation (1) Skip section navigation (2)

Re: Method Question

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Admin(at)MudPortal(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Method Question
Date: 2004-01-31 15:50:20
Message-ID: 20040131073746.K91270@megazone.bigpanda.com (view raw or flat)
Thread:
Lists: pgsql-novice
On Wed, 28 Jan 2004, Ben Burkhart wrote:

> I'm making a layout for a timecard, if a user could clock in/out just once
> per day, it wouldn't be a problem, but I need to assume no limits, I
> currently have it setup
>
>
> CREATE TABLE timecard ('id' SERIAL,'employee' varchar(100),'time'
> timestamp, inorout varchar(5));
>
>  id | emp  |        time         | outorin
> ----+------+---------------------+---------
>   4 | 9826 | 2004-01-27 06:08:52 | i
>   5 | 9826 | 2004-01-27 06:19:54 | o
>   6 | 9826 | 2004-01-27 06:20:05 | i
>   7 | 9826 | 2004-01-27 08:15:13 | o
>
>
> Using that as example of the data, how could I tell how many hours have
> been in between each i and o? This question stumpeth me. Please help.

Well, you'd probably be best off writing a function to go over the rows or
doing this in a front end.

However... This uses a PostgreSQL extension (DISTINCT ON) and probably
could be simpler and probably handles at least some cases incorrectly:

select distinct on (starttime, startemp) employee, starttime,
time-starttime as diff from timecard,(select id as startid, employee as
startemp,time as starttime from timecard where inorout='i') foo where
employee=startemp and time>starttime order by starttime, startemp, time;


In response to

pgsql-novice by date

Next:From: Tom LaneDate: 2004-01-31 17:06:54
Subject: Re: Method Question
Previous:From: Stephan SzaboDate: 2004-01-31 15:23:36
Subject: Re: 'select nextval('seq_name');' in a function ?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group