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 4DD0BD712B for ; Wed, 24 Aug 2005 12:31:12 -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 78268-05 for ; Wed, 24 Aug 2005 15:31:08 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 2798BD6FF5 for ; Wed, 24 Aug 2005 12:31:08 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.13.1/8.13.1) with ESMTP id j7OFV7s4002314 for ; Wed, 24 Aug 2005 15:31:07 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.13.1/8.13.1/Submit) id j7OFOc1r088764 for pgsql-sql@postgresql.org; Wed, 24 Aug 2005 15:24:38 GMT (envelope-from news) From: Zac X-Newsgroups: pgsql.sql Subject: Re: How to join several selects Date: Wed, 24 Aug 2005 17:26:35 +0200 Organization: Hub.Org Networking Services Lines: 26 Message-ID: References: <430C7A21.9020306@openwired.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en In-Reply-To: <430C7A21.9020306@openwired.net> To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=-2.682 required=5 tests=[ALL_TRUSTED=-2.82, AWL=0.138] X-Spam-Level: X-Archive-Number: 200508/229 X-Sequence-Number: 22457 Josep Sanmartí 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. > > Thanks! > SELECT date_trunc('day', start_time) as day, count(name) FROM users WHERE start_time between "startDate" AND "endDate" GROUP BY day;