pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Help with a view
5+ messages / 3 participants
[nested] [flat]

* Help with a view
@ 2005-09-22 17:29 Leif B. Kristensen <leif@solumslekt.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Leif B. Kristensen @ 2005-09-22 17:29 UTC (permalink / raw)
  To: pgsql-sql

I've created a view 'principals' that gives me this output:

pgslekt=> select * from principals where event=15821;
 person | event | place | sort_date  | tag_type
--------+-------+-------+------------+----------
      2 | 15821 |  1152 | 1999-09-17 |        4
      3 | 15821 |  1152 | 1999-09-17 |        4
(2 rows)

How do I go about selecting the "other" person of this event, given that 
I have one of them and the event number? 

I'd also like a view that gives me the two rows combined into one, with 
"person" replaced by p1 and p2.
-- 
Leif Biberg Kristensen
http://solumslekt.org/



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

* Re: Help with a view
@ 2005-09-22 18:03 Dmitri Bichko <dbichko@aveopharma.com>
  2005-09-22 18:12 ` Re: Help with a view Leif B. Kristensen <leif@solumslekt.org>
  0 siblings, 1 reply; 5+ messages in thread

From: Dmitri Bichko @ 2005-09-22 18:03 UTC (permalink / raw)
  To: Leif B. Kristensen <leif@solumslekt.org>; pgsql-sql

SELECT * FROM principals WHERE event = 15821 AND person != 2?

Dmitri

> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org 
> [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Leif B. 
> Kristensen
> Sent: Thursday, September 22, 2005 1:30 PM
> To: pgsql-sql@postgresql.org
> Subject: [SQL] Help with a view
> 
> 
> I've created a view 'principals' that gives me this output:
> 
> pgslekt=> select * from principals where event=15821;
>  person | event | place | sort_date  | tag_type
> --------+-------+-------+------------+----------
>       2 | 15821 |  1152 | 1999-09-17 |        4
>       3 | 15821 |  1152 | 1999-09-17 |        4
> (2 rows)
> 
> How do I go about selecting the "other" person of this event, 
> given that 
> I have one of them and the event number? 
> 
> I'd also like a view that gives me the two rows combined into 
> one, with 
> "person" replaced by p1 and p2.
> -- 
> Leif Biberg Kristensen
> http://solumslekt.org/
> 
> ---------------------------(end of 
> broadcast)---------------------------
> TIP 4: Have you searched our list archives?
> 
               http://archives.postgresql.org
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer



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

* Re: Help with a view
  2005-09-22 18:03 Re: Help with a view Dmitri Bichko <dbichko@aveopharma.com>
@ 2005-09-22 18:12 ` Leif B. Kristensen <leif@solumslekt.org>
  2005-09-22 20:39   ` Re: Help with a view Rajesh Kumar Mallah <mallah.rajesh@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Leif B. Kristensen @ 2005-09-22 18:12 UTC (permalink / raw)
  To: pgsql-sql

On Thursday 22 September 2005 20:03, Dmitri Bichko wrote:

> SELECT * FROM principals WHERE event = 15821 AND person != 2?

Sure, that's a concise answer to what I actually wrote, but it wasn't 
exactly what I intended :)

Basically, what I've got is the first person and the tag_type. I can do 
it with a function from PHP:

function get_spouses($p) {
    $handle = pg_query("select person from principals
                       where event in (select event from principals
                       where person = $p and tag_type = " .MARR. ")
                       order by sort_date");
    $i=0;
    $spouses = array();
    while ($row = pg_fetch_row($handle) {
        if $row[0] != $p
             $spouses[$i++] = $row[0];
    }
    return $spouses;
}

But this is *ugly* ...
-- 
Leif Biberg Kristensen
http://solumslekt.org/



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

* Re: Help with a view
  2005-09-22 18:03 Re: Help with a view Dmitri Bichko <dbichko@aveopharma.com>
  2005-09-22 18:12 ` Re: Help with a view Leif B. Kristensen <leif@solumslekt.org>
@ 2005-09-22 20:39   ` Rajesh Kumar Mallah <mallah.rajesh@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Rajesh Kumar Mallah @ 2005-09-22 20:39 UTC (permalink / raw)
  To: Leif B. Kristensen <leif@solumslekt.org>; +Cc: pgsql-sql

> Basically, what I've got is the first person and the tag_type. I can do
> it with a function from PHP:
>
> function get_spouses($p) {
>     $handle = pg_query("select person from principals
>                        where event in (select event from principals
>                        where person = $p and tag_type = " .MARR. ")
>                        order by sort_date");

What about adding an extra condition ' and person <> $p ?

                       select person from principals as a
                        where event in (select event from principals as b
                        where person = $p and tag_type = " .MARR. ")
                        and  a.person <> $p
                        order by sort_date

>pgslekt=> select * from principals where event=15821;
>person | event | place | sort_date  | tag_type
>--------+-------+-------+------------+----------
>   2 | 15821 |  1152 | 1999-09-17 |        4
>   3 | 15821 |  1152 | 1999-09-17 |        4
(2 rows)
>I'd also like a view that gives me the two rows combined into one, with
>"person" replaced by p1 and p2.

might consider grouping by all other columns except person and use an
aggregate function over person ,


CREATE VIEW blah AS select list(person) as persons , event , place ,
sort_date , tag_type from principals group by event , place ,
sort_date , tag_type ;

(list is a custom function in my database , currently not posted)

not sure though if i got your problem exactly.

regds
mallah.



>     $i=0;
>     $spouses = array();
>     while ($row = pg_fetch_row($handle) {
>         if $row[0] != $p
>              $spouses[$i++] = $row[0];
>     }
>     return $spouses;
> }



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

* Re: Help with a view
@ 2005-09-22 19:10 Dmitri Bichko <dbichko@aveopharma.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Dmitri Bichko @ 2005-09-22 19:10 UTC (permalink / raw)
  To: Leif B. Kristensen <leif@solumslekt.org>; pgsql-sql

I don't understand how this is different:

SELECT person FROM principals
WHERE event IN (SELECT event FROM principals WHERE person = $p AND
tag_type = " .MARR. ")
AND person != $p
ORDER BY sort_date

Or without subselects:

SELECT p1.person
FROM principals p1
JOIN principals p2 USING(event)
WHERE p2 person = $p
	AND p2.tag_type = " .MARR. "
	AND p1.person != $p
ORDER BY sort_date

Am I being confused?

Dmitri

> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org 
> [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Leif B. 
> Kristensen
> Sent: Thursday, September 22, 2005 2:12 PM
> To: pgsql-sql@postgresql.org
> Subject: Re: [SQL] Help with a view
> 
> 
> On Thursday 22 September 2005 20:03, Dmitri Bichko wrote:
> 
> > SELECT * FROM principals WHERE event = 15821 AND person != 2?
> 
> Sure, that's a concise answer to what I actually wrote, but it wasn't 
> exactly what I intended :)
> 
> Basically, what I've got is the first person and the 
> tag_type. I can do 
> it with a function from PHP:
> 
> function get_spouses($p) {
>     $handle = pg_query("select person from principals
>                        where event in (select event from principals
>                        where person = $p and tag_type = " .MARR. ")
>                        order by sort_date");
>     $i=0;
>     $spouses = array();
>     while ($row = pg_fetch_row($handle) {
>         if $row[0] != $p
>              $spouses[$i++] = $row[0];
>     }
>     return $spouses;
> }
> 
> But this is *ugly* ...
> -- 
> Leif Biberg Kristensen
> http://solumslekt.org/
> 
> ---------------------------(end of 
> broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
> 
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer



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


end of thread, other threads:[~2005-09-22 20:39 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-09-22 17:29 Help with a view Leif B. Kristensen <leif@solumslekt.org>
2005-09-22 18:03 Re: Help with a view Dmitri Bichko <dbichko@aveopharma.com>
2005-09-22 18:12 ` Leif B. Kristensen <leif@solumslekt.org>
2005-09-22 20:39   ` Rajesh Kumar Mallah <mallah.rajesh@gmail.com>
2005-09-22 19:10 Re: Help with a view Dmitri Bichko <dbichko@aveopharma.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