Re: combine SQL SELECT statements into one

From: Chris(dot)Ellis(at)shropshire(dot)gov(dot)uk
To: "Igor Neyman" <ineyman(at)perceptron(dot)com>, neilstylz(at)yahoo(dot)com, <pgsql-general(at)postgresql(dot)org>
Subject: Re: combine SQL SELECT statements into one
Date: 2010-02-01 20:39:53
Message-ID: OF6C0E547E.E86C35A0-ON802576BD.0070536A-802576BD.0071C6AE@shropshire.gov.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice pgsql-sql

> > -----Original Message-----
> > From: Chris(dot)Ellis(at)shropshire(dot)gov(dot)uk
> > [mailto:Chris(dot)Ellis(at)shropshire(dot)gov(dot)uk]
> > Sent: Monday, February 01, 2010 4:08 AM
> > To: neilstylz(at)yahoo(dot)com
> > Cc: pgsql-general(at)postgresql(dot)org
> > Subject: Re: combine SQL SELECT statements into one
> >
> >
> > 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.
> >
> > **************************************************************
> > ****************
> >
>
> Original poster asked for the sql that will touch inventory table only
> once.
>
> Your statement (with 3 subqueries) will do it 3 times.
> Igor Neyman
>

---
I think you will find that the poster asked to touch the DATABASE not the
TABLE only once:

'Can this be done with ONE SQL STATEMENT? touching the database
only ONE time?'

While the sugested query might not me as optimised as possible, it
demonstrates a possible method of folding multiple select statements into
one select statement. This seemed
main purpose of this post. I made the assumption that the intent was to
reduce the overhead and latency caused from sending multiple statements.

Chris Ellis

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Hardwick, Joe 2010-02-01 21:25:53 statement_timeout problem
Previous Message Scott Marlowe 2010-02-01 20:25:41 Re: combine SQL SELECT statements into one

Browse pgsql-novice by date

  From Date Subject
Next Message Jasen Betts 2010-02-02 08:54:43 Re: Question about migrating data.
Previous Message Scott Marlowe 2010-02-01 20:25:41 Re: combine SQL SELECT statements into one

Browse pgsql-sql by date

  From Date Subject
Next Message Lee Hachadoorian 2010-02-01 21:14:34 Re: Crosstab Confusion
Previous Message Scott Marlowe 2010-02-01 20:25:41 Re: combine SQL SELECT statements into one