pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How do you write this query?
4+ messages / 4 participants
[nested] [flat]

* How do you write this query?
@ 2002-10-31 18:21 Wei Weng <wweng@kencast.com>
  2002-10-31 19:09 ` Re: How do you write this query? Richard Huxton <dev@archonet.com>
  2002-11-01 07:51 ` Re: How do you write this query? Achilleus Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Wei Weng @ 2002-10-31 18:21 UTC (permalink / raw)
  To: pgsql-sql

I have a table 

                 Table "test"
Column	|            Type        | Modifiers
--------+------------------------+----------
data    | integer                | not null 
data1   | character varying(128) | not null
data2   | character varying(128) | not null

(Note: data is NOT the primary key.)

And 
select * from test 
returns


 data | data1 | data2 
------+-------+-------
    1 | foo   | bar 
    2 | greg  | bar
    3 | pooh  | bar
    4 | dah   | peng

I need a query that returns me the "data1" that satisfies the logic of
the following pseudo code:

1: select data2 into @out from test where data1 = 'pooh'
2: select data1 from test where data2 = @out and data = 3


What do I do?

Thanks!

-- 
Wei Weng
Network Software Engineer
KenCast Inc.





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How do you write this query?
  2002-10-31 18:21 How do you write this query? Wei Weng <wweng@kencast.com>
@ 2002-10-31 19:09 ` Richard Huxton <dev@archonet.com>
  2002-10-31 20:38   ` Re: How do you write this query? Jean-Luc Lachance <jllachan@nsd.ca>
  1 sibling, 1 reply; 4+ messages in thread

From: Richard Huxton @ 2002-10-31 19:09 UTC (permalink / raw)
  To: Wei Weng <wweng@kencast.com>; pgsql-sql

On Thursday 31 Oct 2002 6:21 pm, Wei Weng wrote:
>  data | data1 | data2
> ------+-------+-------
>     1 | foo   | bar
>     2 | greg  | bar
>     3 | pooh  | bar
>     4 | dah   | peng
>
> I need a query that returns me the "data1" that satisfies the logic of
> the following pseudo code:
>
> 1: select data2 into @out from test where data1 = 'pooh'
> 2: select data1 from test where data2 = @out and data = 3

The most literal would be something like:

SELECT t1.data1 FROM test t1
WHERE t1.data=3 AND t1.data2 IN
    (SELECT t2.data2 
    FROM test t2 
    WHERE t2.data1='pooh')

You can probably get away without the t1/t2 stuff but that should make things 
clear.

Since Postgresql isn't very good at optimising IN, you might want to rewrite 
it as an EXISTS query instead - see the manuals and mailing list archives for 
details.

HTH
-- 
  Richard Huxton



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How do you write this query?
  2002-10-31 18:21 How do you write this query? Wei Weng <wweng@kencast.com>
  2002-10-31 19:09 ` Re: How do you write this query? Richard Huxton <dev@archonet.com>
@ 2002-10-31 20:38   ` Jean-Luc Lachance <jllachan@nsd.ca>
  0 siblings, 0 replies; 4+ messages in thread

From: Jean-Luc Lachance @ 2002-10-31 20:38 UTC (permalink / raw)
  To: Richard Huxton <dev@archonet.com>; +Cc: Wei Weng <wweng@kencast.com>; pgsql-sql

Thank goodness for nested select!

select data1 from test where data2 = ( select distinct data2 from test
where data1 = 'pooh') and data = 3;

JLL

Richard Huxton wrote:
> 
> On Thursday 31 Oct 2002 6:21 pm, Wei Weng wrote:
> >  data | data1 | data2
> > ------+-------+-------
> >     1 | foo   | bar
> >     2 | greg  | bar
> >     3 | pooh  | bar
> >     4 | dah   | peng
> >
> > I need a query that returns me the "data1" that satisfies the logic of
> > the following pseudo code:
> >
> > 1: select data2 into @out from test where data1 = 'pooh'
> > 2: select data1 from test where data2 = @out and data = 3
> 
> The most literal would be something like:
> 
> SELECT t1.data1 FROM test t1
> WHERE t1.data=3 AND t1.data2 IN
>     (SELECT t2.data2
>     FROM test t2
>     WHERE t2.data1='pooh')
> 
> You can probably get away without the t1/t2 stuff but that should make things
> clear.
> 
> Since Postgresql isn't very good at optimising IN, you might want to rewrite
> it as an EXISTS query instead - see the manuals and mailing list archives for
> details.
> 
> HTH
> --
>   Richard Huxton
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
> 
> http://www.postgresql.org/users-lounge/docs/faq.html



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How do you write this query?
  2002-10-31 18:21 How do you write this query? Wei Weng <wweng@kencast.com>
@ 2002-11-01 07:51 ` Achilleus Mantzios <achill@matrix.gatewaynet.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Achilleus Mantzios @ 2002-11-01 07:51 UTC (permalink / raw)
  To: Wei Weng <wweng@kencast.com>; +Cc: pgsql-sql

On 31 Oct 2002, Wei Weng wrote:

and yet another equivalent query:

SELECT f1.data1 from test f1,test f2 where f1.data=3 and f1.data2 =
f2.data2 and f2.data1='pooh';

> I have a table
>
>                  Table "test"
> Column	|            Type        | Modifiers
> --------+------------------------+----------
> data    | integer                | not null
> data1   | character varying(128) | not null
> data2   | character varying(128) | not null
>
> (Note: data is NOT the primary key.)
>
> And
> select * from test
> returns
>
>
>  data | data1 | data2
> ------+-------+-------
>     1 | foo   | bar
>     2 | greg  | bar
>     3 | pooh  | bar
>     4 | dah   | peng
>
> I need a query that returns me the "data1" that satisfies the logic of
> the following pseudo code:
>
> 1: select data2 into @out from test where data1 = 'pooh'
> 2: select data1 from test where data2 = @out and data = 3
>
>
> What do I do?
>
> Thanks!
>
> --
> Wei Weng
> Network Software Engineer
> KenCast Inc.
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>

==================================================================
Achilleus Mantzios
S/W Engineer
IT dept
Dynacom Tankers Mngmt
Nikis 4, Glyfada
Athens 16610
Greece
tel:    +30-10-8981112
fax:    +30-10-8981877
email:  achill@matrix.gatewaynet.com
        mantzios@softlab.ece.ntua.gr




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2002-11-01 07:51 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-10-31 18:21 How do you write this query? Wei Weng <wweng@kencast.com>
2002-10-31 19:09 ` Richard Huxton <dev@archonet.com>
2002-10-31 20:38   ` Jean-Luc Lachance <jllachan@nsd.ca>
2002-11-01 07:51 ` Achilleus Mantzios <achill@matrix.gatewaynet.com>

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