pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Glaesemann <grzm@myrealbox.com>
To: Sean Davis <sdavis2@mail.nih.gov>
Cc: Josep SanmartĂ­ <josep.sanmarti@openwired.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: How to join several selects
Date: Wed, 24 Aug 2005 23:14:22 +0900
Message-ID: <9C26B82A-ED19-459B-924F-0F91AABF7DD9@myrealbox.com> (raw)
In-Reply-To: <BF31F66E.D0DF%sdavis2@mail.nih.gov>
References: <BF31F66E.D0DF%sdavis2@mail.nih.gov>


On Aug 24, 2005, at 11:03 PM, Sean Davis wrote:

> On 8/24/05 9:46 AM, "Josep SanmartĂ­" <josep.sanmarti@openwired.net>  
> 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"

I'm guessing he wants something more like this, so he knows which  
period is which:

SELECT count_1, count_2
FROM (
     SELECT COUNT(name) AS count_1
     FROM users
     WHERE start_time BETWEEN startDate AND startDate + 1
     ) as period_1
CROSS JOIN (
     SELECT COUNT(name) AS count_2
     FROM users
     WHERE start_time BETWEEN startDate + 1 AND startDate + 2
     ) as period_2

Though, you could do the same thing using UNION like this:

SELECT 'period_1'::text as period, COUNT(name) AS num_of_users
FROM users
WHERE start_time BETWEEN startDate AND startDate + 1

UNION

SELECT 'period_2'::text as period, COUNT(name) AS num_of_users
FROM users
WHERE start_time BETWEEN startDate + 1 AND startDate + 2

And of course, using EXPLAIN ANALYZE will help decide which is more  
performant.

Michael Glaesemann
grzm myrealbox com





view thread (5+ messages)  latest in thread

Message-ID: <9C26B82A-ED19-459B-924F-0F91AABF7DD9@myrealbox.com>
Permalink:  ../9C26B82A-ED19-459B-924F-0F91AABF7DD9@myrealbox.com/
Also on:    postgresql.org/message-id/9C26B82A-ED19-459B-924F-0F91AABF7DD9@myrealbox.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: grzm@myrealbox.com, sdavis2@mail.nih.gov, josep.sanmarti@openwired.net
  Subject: Re: How to join several selects
  In-Reply-To: <9C26B82A-ED19-459B-924F-0F91AABF7DD9@myrealbox.com>
  References: <BF31F66E.D0DF%sdavis2@mail.nih.gov> <9C26B82A-ED19-459B-924F-0F91AABF7DD9@myrealbox.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox