C
Carl Colijn
Hi group,
I want to perform an inner join between two tables.
The first (named 'Data') has some data columns and two classification
columns ('Period' = Date/Time, and 'SequenceNr' = Long) that identify the
type of record. If the record is actual, then the classification columns
contains NULL's. If the record is historical, then the record contains the
correct date for the period and a sequence number (there can be more than
one set of historical records per period).
The second table (named 'Conf_History') is used as a configuration table to
determine the period and sequence nr. to show throughout the database's
forms. It thus contains the same two columns 'Period' and 'SequenceNr'. It
holds exactly one record.
Now comes the tricky part. I want to base my forms on a RWOP query to
access the data in the backend database, but they should only show the
correct set of records (according to the 'Conf_History' table). My first
try was to use an inner join in the query:
SELECT Data.*
INNER JOIN Conf_History
ON Data.Period = Conf_History.Period
AND Data.SequenceNr = Conf_History.SequenceNr
but this didn't work because NULL = NULL doesn't work. The next attempt
was:
SELECT Data.*
FROM Data, Conf_History
WHERE Data.Period = Conf_History.Period
AND Data.SequenceNr = Conf_Historiy.SequenceNr
OR (Data.Period IS NULL AND Conf_Historiy.Period IS NULL)
but this seems inefficient (making a cross join first combined with a more
convoluted WHERE clause). And then the result set of these queries is not
appendable nor modifyable (a requirement for my forms).
The alternative I came up with is to use a custom VBA function to check each
record:
SELECT *
FROM Data
WHERE IsCorrectPeriod(Period, SequenceNr)
in combination with the VBA routine:
Private g_dPeriod As Date ' These get initialized on DB
startup
Private g_nSequenceNr As Long ' These get initialized on DB startup
Private g_bIsCurrent As Boolean ' These get initialized on DB startup
Public Function IsCorrectPeriod(dPeriod As Variant, nSequenceNr As
Variant)
If IsNull(dPeriod) And g_bIsCurrent Then
IsCorrectPeriod = True
Else
IsCorrectPeriod = (dPeriod = g_dPeriod) And _
(g_nSequenceNr = nSequenceNr)
End If
End Function
but I can imagine this gives even more overhead... The resultset of the
query is appendable and modifyable however, exacty as I wanted.
And now for the $1.000.000 question: is there an alternative that given me
better performance and/or less complicated queries?
Thanks in advance,
Carl Colijn
I want to perform an inner join between two tables.
The first (named 'Data') has some data columns and two classification
columns ('Period' = Date/Time, and 'SequenceNr' = Long) that identify the
type of record. If the record is actual, then the classification columns
contains NULL's. If the record is historical, then the record contains the
correct date for the period and a sequence number (there can be more than
one set of historical records per period).
The second table (named 'Conf_History') is used as a configuration table to
determine the period and sequence nr. to show throughout the database's
forms. It thus contains the same two columns 'Period' and 'SequenceNr'. It
holds exactly one record.
Now comes the tricky part. I want to base my forms on a RWOP query to
access the data in the backend database, but they should only show the
correct set of records (according to the 'Conf_History' table). My first
try was to use an inner join in the query:
SELECT Data.*
INNER JOIN Conf_History
ON Data.Period = Conf_History.Period
AND Data.SequenceNr = Conf_History.SequenceNr
but this didn't work because NULL = NULL doesn't work. The next attempt
was:
SELECT Data.*
FROM Data, Conf_History
WHERE Data.Period = Conf_History.Period
AND Data.SequenceNr = Conf_Historiy.SequenceNr
OR (Data.Period IS NULL AND Conf_Historiy.Period IS NULL)
but this seems inefficient (making a cross join first combined with a more
convoluted WHERE clause). And then the result set of these queries is not
appendable nor modifyable (a requirement for my forms).
The alternative I came up with is to use a custom VBA function to check each
record:
SELECT *
FROM Data
WHERE IsCorrectPeriod(Period, SequenceNr)
in combination with the VBA routine:
Private g_dPeriod As Date ' These get initialized on DB
startup
Private g_nSequenceNr As Long ' These get initialized on DB startup
Private g_bIsCurrent As Boolean ' These get initialized on DB startup
Public Function IsCorrectPeriod(dPeriod As Variant, nSequenceNr As
Variant)
If IsNull(dPeriod) And g_bIsCurrent Then
IsCorrectPeriod = True
Else
IsCorrectPeriod = (dPeriod = g_dPeriod) And _
(g_nSequenceNr = nSequenceNr)
End If
End Function
but I can imagine this gives even more overhead... The resultset of the
query is appendable and modifyable however, exacty as I wanted.
And now for the $1.000.000 question: is there an alternative that given me
better performance and/or less complicated queries?
Thanks in advance,
Carl Colijn