Re: [SQL] multi table import from 1 denormalized source table

2010-11-16 Thread Jasen Betts
On 2010-11-16, Andreas  wrote:
> Hi,
>
> I frequently get tables from spreadsheets to import into the DB.
>
> Usually it looks like this:
> A1, A2, A3, A4,   B1, B2, B3,   with optional C1, C2,   D1, D2, ...
>
> and there is a 1:n relation between A and B.
> If provieded the C would be 1:1 to A and D 1:1 to B.
>
> Up until now I let a VBA script order the source table by A, then scan 
> the table line by line and create a new entry in the target table A* and 
> fetch its serial ID everytime the script figures that A changed.
> With this IDa create 1 C* and as many B*s until A changes again ... and 
> of course fetch IDb to attach the D* records with a foreign key column.
>
> Now I'm trying to get away w/o the VBA stuff.
>
> Is there a clever way to split such denormalized sources while still 
> obtaining the needed IDs to connect everything?

create a temporary table and put the IDS in there.

then do a select distinct to populate table B, 
do an update from to put the IDs from B into the temp table
then do a select to populate table A

etc...



-- 
⚂⚃ 100% natural

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


[SQL] Sum up network events by type, interval and network

2010-11-16 Thread Axel Rau

Hi all,

For each event of each type in each interval I want to compute the sum  
of events referencing the same relayNet (via host) and insert/update 1  
row in SumOfEvents.
All intervals start at a time, minimum 5 minutes in the past, rounded  
down modulo 5 minutes like:


SELECT DATE_TRUNC('MINUTES', NOW () - ('0:' || (SELECT  
(EXTRACT('MINUTE' FROM NOW())::INT % 5) + 5 ) || ':0')::INTERVAL);




CREATE TABLE host (
  idSERIAL  PRIMARY KEY,
  relayNetFKINT
REFERENCES relayNet
ON DELETE CASCADE
)

CREATE TABLE event (
  idSERIAL  PRIMARY KEY,
  type  CHAR NOT NULL
CHECK ( type IN ('C', 'A', 'D', 'S', 'R') ),
  timeOfEvent   timestamp   NOT NULL
DEFAULT NOW(),
  hostfkINT  NOT NULL
REFERENCES host
ON DELETE CASCADE
)

CREATE TABLE relayNet (
  idSERIAL  PRIMARY KEY,
  name  TEXTNOT NULL UNIQUE
)

CREATE TYPE eventIntervals AS ENUM ('5m', '30m', '3h', '24h', '30d');
CREATE TABLE SumOfEvents (
  idSERIAL  PRIMARY KEY,
  type  CHAR NOT NULL
CHECK ( type IN ('C', 'A', 'D', 'S', 'R') ),
  startTime timestamp   NOT NULL,
  interval  eventIntervals  NOT NULL,
  value INT,-- sum
  relayNetFKINT
REFERENCES relayNet
ON DELETE CASCADE,

  UNIQUE (type, interval, relayNetFK)
)

Can this be done w/o procedural code?
Any hints?

Thanks, Axel
---
axel@chaos1.de  PGP-Key:29E99DD6  +49 151 2300 9283  computing @  
chaos claudius



--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql