Received: from shaked.cc.openu.ac.il (shaked.cc.openu.ac.il [147.233.145.65]) by hub.org (8.8.8/8.7.5) with ESMTP id JAA08663 for ; Wed, 17 Jun 1998 09:08:00 -0400 (EDT) Received: from [147.233.159.109] (herouth@mac-herouth.pc.openu.ac.il [147.233.159.109]) by shaked.cc.openu.ac.il (8.8.5/8.8.5) with ESMTP id QAA20956; Wed, 17 Jun 1998 16:01:59 +0300 (IDT) X-Sender: herouth@shaked.cc.openu.ac.il Message-Id: In-Reply-To: <199806171233.IAA01017@bentley.eso.mc.xerox.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Date: Wed, 17 Jun 1998 16:02:15 +0300 To: kcolagio@wc.eso.mc.xerox.com (Kevin Colagio), pgsql-sql@postgreSQL.org From: Herouth Maoz Subject: Re: [SQL] Time related question... At 15:33 +0300 on 17/6/98, Kevin Colagio wrote: > > My question is: what is the SQL statement that will allow me to find: > 1) a list of the calls where the difference between the dateentered and > dateclosed is less than (say) 3 days. Here is an example: testing=> \d example4 Table = example4 +--------------------------+------------------------------+-------+ | Field | Type | Length| +--------------------------+------------------------------+-------+ | entered | datetime | 8 | | closed | datetime | 8 | +--------------------------+------------------------------+-------+ testing=> SELECT * FROM example4; entered |closed ----------------------------+---------------------------- Wed Jan 14 00:00:00 1998 IST|Thu Jan 15 00:00:00 1998 IST Wed Jan 14 00:00:00 1998 IST|Sun Jan 18 00:00:00 1998 IST Thu Mar 19 23:00:00 1998 IST|Tue Feb 17 00:00:00 1998 IST Wed Apr 01 00:00:00 1998 IDT|Thu Apr 01 00:00:00 1999 IST Mon Jun 01 00:00:00 1998 IDT|Wed Jun 03 00:00:00 1998 IDT (5 rows) testing=> SELECT * testing-> FROM example4 testing-> WHERE (closed - entered) < '3 days'; entered |closed ----------------------------+---------------------------- Wed Jan 14 00:00:00 1998 IST|Thu Jan 15 00:00:00 1998 IST Thu Mar 19 23:00:00 1998 IST|Tue Feb 17 00:00:00 1998 IST Mon Jun 01 00:00:00 1998 IDT|Wed Jun 03 00:00:00 1998 IDT (3 rows) If you're wondering why you got the second row, it's because it has an entered date which is greater than the closed date. A negative difference is less than 3 days... So you should actually make sure this doesn't happen, like this: testing=> SELECT * testing-> FROM example4 testing-> WHERE closed > entered testing-> AND (closed - entered) < '3 days'; entered |closed ----------------------------+---------------------------- Wed Jan 14 00:00:00 1998 IST|Thu Jan 15 00:00:00 1998 IST Mon Jun 01 00:00:00 1998 IDT|Wed Jun 03 00:00:00 1998 IDT (2 rows) Herouth -- Herouth Maoz, Internet developer. Open University of Israel - Telem project http://telem.openu.ac.il/~herutma