Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Newbie, Create groups of chained records

$
0
0
Hi,
(Newbie, Postgresql 9.1)

I have a problem in a table that has some records that link to one another and need to be grouped.
Some records are 'sons' of other records so I can order them into a chain but I don't know how to
write a Group ID onto group of chained records.

Here's the story :
Acme Company supplies office cleaning personnel to Companies Alpha, Beta, Gamma ....
Acme and her client Companies have one or more Contracts to supply a Cleaner - or a Replacement Cleaner.
Acme does not have to renew the contract if it sends a Replacement Cleaner but the change does appear in the Cleaner Activity table.

Alas, Acme has lost the Contracts table and is trying to contruct a new one based on the data in the Cleaner Activity records : i.e. who worked when.
The Contracts field in the Cleaner Activity records was wiped by gremlins.
As the Contract table is gone for good, Acme is willing to give new IDs to the Contacts.

Priority number 1 is, for each contract, to
find the Start Date and Finishing Date for the first Cleaner (if there was only ever one cleaner in the Contract)
- or -
the Start Date for the first Cleaner and the Finishing Date for the last of all the replacement Cleaners.

--- Please see the Cleaner Activity.png attached.

This data contains evidence of two Contracts :

The first discernable Contract
- concerns Cleaners A, B, C and D.
- started on 01/01/2008 and has no Date Finished, so we can conclude that it is ongoing.

The second discernable Contract
- concerns Cleaners E, F and G.
- started on 01/01/2009 and finished on 31/12/2011.

The original Cleaner starts with the new Contract does get replaced over the duration of the Contract they all came to work under the terms of service of the same Contract.

I would like to be able to update the Contract ID field with a sequence :
1 for the first Contract (and its Group of Cleaners),
2 for the second Contract (and its Group of Cleaners)
3 etc.....
but I don't know how to do it.

I thought of GROUP BY but I have no common value in any field.
I thought of Window fuctions but I have no common value to PARTITION on ...
So I imagine that the first step will be to get a common value into a field.
So I now have an empty Contract ID field ready and waiting and no idea how to fill it.

I will be happy when I am able to produce this new Contract table.

--- Please see the Contracts.png attached.

Any help appreciated,
TIA

F. Neurth

Attached Images
File Type: png Cleaner Activity.png (13.0 KB)
File Type: png Contracts.png (5.4 KB)

Viewing all articles
Browse latest Browse all 13329

Trending Articles