Possible fetch spec (or custom SQL)?

Ashley Aitken <[email protected]> Fri, 17 Sep 2004 00:17:10 +0800
Newsgroups gmane.comp.web.webobjects.eof
Message-ID <[email protected]>
To the fetch spec / SQL wizards:

Is this possible?

For the following entities, attributes, and relationships:

EntityA has a primary key and a to-many relationship called "items" to 
EntityB
EntityB has a primary key and a foreign key and attributes attribute1 
and attribute2
Some EntityB items may share the same value for attribute1
Within each set of those EntityB items attribute2 is a unique integer 
value

To construct a fetch spec (in EOModeler or programmatically) or custom 
SQL to do the following:

Fetch all the EntityB items in the relationship "items" for a given 
EntityA item such that for each of the different values of attribute1 
the item with the maximum value of attribute2 less than or equal to a 
specified value is chosen.  There will always be a value of attribute2 
less than or equal to a the specified value for each of the values of 
attribute 1.

For example:

EntityA (Primary Key)
(1)
(2)

EntityB (Primary Key, Foreign Key for EntityA, attribute1, attribute2)
(1, 1, 3, 1)
(2, 2, 3, 4)
(3, 1, 5, 1)
(4, 1, 5, 2)
(5, 2, 1, 6)
(6, 1, 6, 0)
(7, 1, 6, 1)
(9, 1, 6, 4)
(10, 1, 9, 2)
(11, 1, 9, 1)

A fetch for items of (1) with specified value 9 should yield
(1, 1, 3, 1)  which is the EntityB chosen for attribute1 = 3
(4, 1, 5, 2) which is the EntityB chosen for attribute1 = 5
(9, 1, 6, 4) which is the EntityB chosen for attribute1 = 6
(10, 1, 9, 2) which is the EntityB chosen for attribute1 = 9

A fetch for items of (1) with specified value 1 should yield
(1, 1, 3, 1)
(3, 1, 5, 1)
(6, 1, 6, 0)
(10, 1, 9, 1)

It needs to be one fetch spec or custom SQL so that I can arrange to 
other entities related to the resulting set of items.

Thanks for any help - even if it is to say that it's not possible.

Cheers,
Ashley.

--
Ashley Aitken
Perth, Western Australia
mrhatken at mac dot com