Re: combine SQL SELECT statements into one

From: Chris(dot)Ellis(at)shropshire(dot)gov(dot)uk
To: neilstylz(at)yahoo(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: combine SQL SELECT statements into one
Date: 2010-02-01 09:08:17
Message-ID: OF1DFE3081.6BFBD791-ON802576BD.0031A099-802576BD.00327569@shropshire.gov.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice pgsql-sql

Hi

pgsql-general-owner(at)postgresql(dot)org wrote on 02/01/2010 07:36:55 AM:

> Good Evening, Good Morning Wherever you are whenever you may be reading
this.
>
>
snip
>
> count1 | count2 | count3
> -------------------------------
> 2 2 4
>
> Can this be done with ONE SQL STATEMENT? touching the database only ONE
time?

You can do the following:

SELECT
(SELECT COUNT(distinct model) FROM inventory WHERE modified >=
'2010-02-01') AS "COUNT_1",
(SELECT COUNT(distinct model) FROM inventory WHERE modified >=
'2010-01-20') AS "COUNT_2",
(SELECT COUNT(distinct model) FROM inventory WHERE modified >=
'2010-01-01') AS "COUNT_3"
;

PostgreSQL allows sub-queries in the select list as long as the sub-query
returns one column

Job done

> Please let me know.
>
> Thanx> :)
> NEiL
>

Chris Ellis
******************************************************************************
If you are not the intended recipient of this email please do not send it on
to others, open any attachments or file the email locally.
Please inform the sender of the error and then delete the original email.
For more information, please refer to http://www.shropshire.gov.uk/privacy.nsf
******************************************************************************
Help prevent the spread of swine flu. CATCH IT. BIN IT. KILL IT.
******************************************************************************

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Raymond O'Donnell 2010-02-01 09:33:12 Re: How to test my new install
Previous Message A. Kretschmer 2010-02-01 09:03:28 Re: combine SQL SELECT statements into one

Browse pgsql-novice by date

  From Date Subject
Next Message msi77 2010-02-01 10:10:51 Re: combine SQL SELECT statements into one
Previous Message A. Kretschmer 2010-02-01 09:03:28 Re: combine SQL SELECT statements into one

Browse pgsql-sql by date

  From Date Subject
Next Message msi77 2010-02-01 10:10:51 Re: combine SQL SELECT statements into one
Previous Message A. Kretschmer 2010-02-01 09:03:28 Re: combine SQL SELECT statements into one