Subject Re: Database design : data versioning
Author zedalaye
Hi Svein,

> Problem 1 - efficiency:
>
> See if something similar to this procedure is any quicker (it won't
> work, since you are referring to the alias 'a', which isn't
> mentioned anywhere):

Sorry for the "a" aliased table, congratulations for having found out
something useable from such a... stupid statement.

Anyway, thank you very much for your answer, your request is one of
the more efficient I, and people who helpe me found :

select
p.PRD_ID,
p.PRD_VERSION,
p.PRD_NAME,
p.PRD_FAMILY,
f.FAM_NAME,
from PRODUCTS p
join FAMILIES f on (f.FAM_ID = p.PRD_FAMILY)
where not exists (
select * from PRODUCTS p2
where p2.PRD_ID=p.PRD_ID
and p2.PRD_VERSION <= :FOR_VERSION
and p2.PRD_VERSION > p.PRD_VERSION
)
and not exists (
select * from FAMILIES f2
where f2.FAM_ID=f.FAM_ID
and f2.FAM_VERSION <= :FOR_VERSION
and f2.FAM_VERSION > f.FAM_VERSION
)

There's just a little "bug" : I cannot retrieve another "old" version
of the database without adding a test on PRD_VERSION, your code can
only give me the latest version :

select
p.PRD_ID,
p.PRD_VERSION,
p.PRD_NAME,
p.PRD_FAMILY,
f.FAM_NAME,
from PRODUCTS p
join FAMILIES f on (f.FAM_ID = p.PRD_FAMILY)
where

/* Here */

(p.PRD_VERSION <= :FOR_VERSION)

/* /Here */

and not exists (
select * from PRODUCTS p2
where p2.ART_ID=a.ART_ID and p2.ART_VERSION > p.ART_VERSION
and p2.ART_VERSION <= :FOR_VERSION
)
and not exists(
select * from FAMILIES f2
where f2.FAM_ID=f.FAM_ID and f2.FAM_VERSION > f.FAM_VERSION
and f2.FAM_VERSION <= :FOR_VERSION
)

I have had another suggestion by Philippe that is the most efficient,
but I don't like it very much because it is not... sexy and I did'nt
find how to use it for joined tables :

select
p.PRD_ID,
max(p.PRD_VERSION) as PRD_VERSION,
max(p2.PRD_NAME) as PRD_NAME,
max(p2.PRD_FAMILY) as PRD_FAMILLY,
from PRODUCTS p
join PRODUCTS p2 on (
p.PRD_ID = p2.PRD_ID and p.PRD_VERSION = p2.PRD_VERSION
)
where p.PRD_VERSION <= :FOR_VERSION

It would be nice to find a way to write it without 'maxes' because
it's easier to read.

Regards,

--
Pierre Y.