X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C59B1D76A0 for ; Wed, 24 Aug 2005 11:03:29 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 10289-06 for ; Wed, 24 Aug 2005 14:03:26 +0000 (GMT) Received: from wimpy.net.nih.gov (mailfwd.nih.gov [128.231.88.106]) by svr1.postgresql.org (Postfix) with ESMTP id B8719D7363 for ; Wed, 24 Aug 2005 11:03:24 -0300 (ADT) Received: from wimpy.net.nih.gov (localhost [127.0.0.1]) by wimpy.net.nih.gov (8.12.11/8.11.7) with ESMTP id j7OE3Sgq006376 for ; Wed, 24 Aug 2005 10:03:29 -0400 (EDT) Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by wimpy.net.nih.gov (8.12.11/8.11.7) with ESMTP id j7OE3Rv7006367; Wed, 24 Aug 2005 10:03:27 -0400 (EDT) User-Agent: Microsoft-Entourage/10.1.4.030702.0 Date: Wed, 24 Aug 2005 10:03:26 -0400 Subject: Re: How to join several selects From: Sean Davis To: Josep Sanmart=?ISO-8859-1?B?7Q==?= , Message-ID: In-Reply-To: <430C7A21.9020306@openwired.net> Mime-version: 1.0 Content-type: text/plain; charset="ISO-8859-1" Content-transfer-encoding: quoted-printable X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.008 required=5 tests=[AWL=0.008] X-Spam-Level: X-Archive-Number: 200508/225 X-Sequence-Number: 22453 On 8/24/05 9:46 AM, "Josep Sanmart=ED" wrote: > Hello, > I have a 'big' problem: > I have the following table users(name, start_time, end_time), a new row > is set whenever a user logs into a server. I want to know how many > users have logged in EVERYDAY between 2 different dates. The only idea > that I have is making several select (one for each day): > SELECT COUNT(name) FROM users WHERE start_time between "startDate" > and "startDate+1" > SELECT COUNT(name) FROM users WHERE start_time between "startDate+1" > and "startDate+2" > ... > I would like to know if its possible to make it in 1 sql statement or > just which is the best efficient way to solve it. > By the way, I use Postgres 7.4. See: http://www.postgresql.org/docs/8.0/interactive/sql-select.html#SQL-UNION like: SELECT COUNT(name) FROM users WHERE start_time between "startDate" and "startDate+1" union SELECT COUNT(name) FROM users WHERE start_time between "startDate+1" and "startDate+2" Sean