help with a view (join-query) - Mailing list pgsql-novice

From
Subject help with a view (join-query)
Date
Msg-id 03f601c64f71$e172be50$0201a8c0@iwing
Whole thread Raw
Responses Re: help with a view (join-query)  (Bruno Wolff III <bruno@wolff.to>)
List pgsql-novice
hi list
 
currently in our database, a "person" contains a relation to one or more "events". i now need a view where each person is listed exactly once, and to each person only one (of possible more) events is listed. it does not matter which ones of the events are shown. when doing a normal JOIN, the resultset might return more than one record per person (for each event one line).
is there an easy way to build a query that fullfills my needs? i'd rather not check programmatically if i already listed the person or not.
 
here's some sample data:
 
table person
(person_name | person_id):
------------------
thomas | 1
barbara | 2
samuel | 3
 
table events
(event_name | event_id):
------------------
event1 | 1
event2 | 2
event3 | 3
 
table personevents
(person_id | event_id) :
----------------------------
1 | 1
1 | 2
1 | 3
2 | 3
3 | 1
3 | 2
 
one possible result i need would look like the following
(person_name | person_id | event_name | event_id)
---------------------------------
thomas | 1 | event1 | 1
barbara | 2 | event3 | 3
samuel | 3 | event1 | 1
 
while all the join-variants i know of return something like
(person_name | person_id | event_name | event_id):
----------------------------------
thomas | 1 | event1 | 1
thomas | 1 | event2 | 2
thomas | 1 | event3 | 3
barbara | 2 | event3 | 3
samuel | 3 | event1 | 1
samuel | 3 | event2 | 2
 
 
i hope my explanations are understandable :-)
 
thanks for any hints,
thomas
 
 
 
 
 
 

pgsql-novice by date:

Previous
From: Alan Hodgson
Date:
Subject: Re: Indexes and inheritance
Next
From: "Pierre Thibaudeau"
Date:
Subject: Re: Indexes and inheritance