[SQL] Finding multiple events of the same kind

2006-06-11 Thread Leif B. Kristensen
I've got two tables:

CREATE TABLE events (
event_idINTEGER PRIMARY KEY,
tag_fk  INTEGER NOT NULL REFERENCES tags (tag_id),
place_fkINTEGER NOT NULL REFERENCES places (place_id),
event_date  CHAR(18) NOT NULL DEFAULT '31',
sort_date   DATE NOT NULL DEFAULT '40041024BC',
event_note  TEXT NOT NULL DEFAULT ''
);

CREATE TABLE participants (
person_fk   INTEGER NOT NULL REFERENCES persons (person_id),
event_fkINTEGER NOT NULL REFERENCES events (event_id) ON DELETE 
CASCADE,
is_principal BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (person_fk, event_fk)
);

The table "participants" is of course a many-to-many relation 
between "events" and "persons". My problem is that it's entirely 
possible to insert eg. multiple birth events for one person, and I'd 
like to be able to spot these.

I've made this function that will return a birth date, but it will of 
course be somewhat undefined in case of multiple events (tag_fk 
2=birth, 62=stillbirth, 1035="guesstimated" birth).

CREATE OR REPLACE FUNCTION get_pbdate(INTEGER) RETURNS TEXT AS $$
DECLARE
pb_date TEXT;
BEGIN
SELECT event_date INTO pb_date FROM events, participants
WHERE events.event_id = participants.event_fk
AND participants.person_fk = $1
AND events.tag_fk in (2,62,1035)
AND participants.is_principal IS TRUE;
RETURN COALESCE(pb_date,'31');
END;
$$ LANGUAGE plpgsql;

The originating database had a field for "primary" event, along with 
some business logic for deciding between multiple events of the same 
type, but I don't want to maintain something like that. I'll rather run 
a report spotting persons with multiple birth events. Any ideas?
-- 
Leif Biberg Kristensen | Registered Linux User #338009
http://solumslekt.org/ | Cruising with Gentoo/KDE

---(end of broadcast)---
TIP 5: don't forget to increase your free space map settings


Re: [SQL] Finding multiple events of the same kind

2006-06-11 Thread Frank Bax

At 08:53 AM 6/11/06, Leif B. Kristensen wrote:


I've got two tables:

CREATE TABLE events (
event_idINTEGER PRIMARY KEY,
tag_fk  INTEGER NOT NULL REFERENCES tags (tag_id),
place_fkINTEGER NOT NULL REFERENCES places (place_id),
event_date  CHAR(18) NOT NULL DEFAULT '31',
sort_date   DATE NOT NULL DEFAULT '40041024BC',
event_note  TEXT NOT NULL DEFAULT ''
);

CREATE TABLE participants (
person_fk   INTEGER NOT NULL REFERENCES persons (person_id),
event_fkINTEGER NOT NULL REFERENCES events (event_id) ON DELETE
CASCADE,
is_principal BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (person_fk, event_fk)
);

The table "participants" is of course a many-to-many relation
between "events" and "persons". My problem is that it's entirely
possible to insert eg. multiple birth events for one person, and I'd
like to be able to spot these.



Something like this should get a list of person_fk values that have more 
than one birth date:


SELECT participants.person_fk, count(participants.person_fk) FROM events, 
participants

   WHERE events.event_id = participants.event_fk
AND events.tag_fk in (2,62,1035)
   GROUP BY participants.person_fk HAVING count(participants.person_fk) > 1


---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

  http://www.postgresql.org/docs/faq


Re: [SQL] Finding multiple events of the same kind

2006-06-11 Thread Leif B. Kristensen
On Sunday 11. June 2006 15:27, Frank Bax wrote:
>SELECT participants.person_fk, count(participants.person_fk) FROM
> events, participants
>    WHERE events.event_id = participants.event_fk
>         AND events.tag_fk in (2,62,1035)
>    GROUP BY participants.person_fk HAVING
> count(participants.person_fk) > 1

That worked like a charm! Thank you very much!
-- 
Leif Biberg Kristensen | Registered Linux User #338009
http://solumslekt.org/ | Cruising with Gentoo/KDE

---(end of broadcast)---
TIP 1: if posting/reading through Usenet, please send an appropriate
   subscribe-nomail command to [EMAIL PROTECTED] so that your
   message can get through to the mailing list cleanly


Re: [SQL] Finding multiple events of the same kind

2006-06-11 Thread Richard Broersma Jr
> On Sunday 11. June 2006 15:27, Frank Bax wrote:
> >SELECT participants.person_fk, count(participants.person_fk) FROM
> > events, participants
> >    WHERE events.event_id = participants.event_fk
> >         AND events.tag_fk in (2,62,1035)
> >    GROUP BY participants.person_fk HAVING
> > count(participants.person_fk) > 1
> 
> That worked like a charm! Thank you very much!
> -- 

Also,  you could create a unique column constraint that would prevent multiply 
instances of the
same person in the participants table.

Regards,

Richard Broersma Jr.

---(end of broadcast)---
TIP 4: Have you searched our list archives?

   http://archives.postgresql.org


[SQL] remove

2006-06-11 Thread Owen Funkhouser

---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

   http://www.postgresql.org/docs/faq


Re: [SQL] Finding multiple events of the same kind

2006-06-11 Thread Leif B. Kristensen
On Sunday 11. June 2006 20:36, Richard Broersma Jr wrote:
>Also,  you could create a unique column constraint that would prevent
> multiply instances of the same person in the participants table.

I have considered that as well. But as my front end main view looks like 
a "structured document" in a browser window, ie. with events and their 
associated notes and sources, it's sometimes very convenient to enter 
and view duplicate events. It can be of immense help in a critical 
evaluation of sources. Here's a screenshot of the main view: 

-- 
Leif Biberg Kristensen | Registered Linux User #338009
http://solumslekt.org/ | Cruising with Gentoo/KDE

---(end of broadcast)---
TIP 4: Have you searched our list archives?

   http://archives.postgresql.org


[SQL] COPY to table with array columns (Longish)

2006-06-11 Thread Phillip Smith








Hi All,

 

Hope someone can help me – our main company system
runs on Raining Data PICK/D3 (if anyone familiar with it) which stores records
in it’s “tables” as variable length items. Every item has a
unique Primary Key (per table) then each item can have a variable number of
fields. These fields are delimited by Char 254, then each field can have
sub-values delimited by Char 253, then sub-sub-values delimited by Char 252.

 

Anyway, we are trying to export everything to Postgres for
reporting and querying etc (not to actually run the system…. Yet) and
hasn’t been a problem so far – everything like stock and purchase
orders, sales orders etc can pretty easily be turned in to a flat file with
standard number of columns and consistent data. We truncate every table each
night then import that latest TSV export from D3 using a COPY command.

 

The problem arises with tables like our SYS table which
store generic system data, so one record could have 3 fields, but the next
could have 300. The only way I can work out how to export multi-valued data like
this to Postgres is to use an array column. So the table has 2 columns –
the pkey and a data array.

 

How do I get this imported to the truncated table each
night? At the moment I think my best option is to modify the export for the SYS
table to call PSQL and use standard SQL INSERT statements to directly insert it
instead of exporting to a flat file, then import to Postgres.

 

Thanks all,

-p

 

For those who are interested, or if it might help, here’s
a rough comparison of the database structure of D3:

Windows   = PICK/D3 = Postgres

Drive     = Account = Database

Directory     = File    = Table

File      = Item    = Row

Line in text file = Attribute   = Field

(none)    = Value   = Array
Element (?)

(none)    = Sub
Value   = (none?)

 

Phillip Smith

IT Coordinator

Weatherbeeta P/L

8 Moncrief Rd

Nunawading, VIC, 3131

AUSTRALIA

 

E. [EMAIL PROTECTED]

 





***Confidentiality and Privilege Notice***

The material contained in this message is privileged and confidential to
the addressee.  If you are not the addressee indicated in this message or
responsible for delivery of the message to such person, you may not copy
or deliver this message to anyone, and you should destroy it and kindly
notify the sender by reply email.

Information in this message that does not relate to the official business
of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta.
Weatherbeeta, its employees, contractors or associates shall not be liable
for direct, indirect or consequential loss arising from transmission of this
message or any attachments