Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / July 2006

Tip: Looking for answers? Try searching our database.

Evaluate Next Record In Query?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David - 27 Jul 2006 20:36 GMT
Hello,

I have a table with pay periods as a field.  I can sort these records by pay
period.  Is there any way to evaluate the next record, in order to create a
field in a query which tells me whether the gap between pay periods is 1 week
or 2 weeks (through evaluating the next record)?
Signature

David

Michel Walsh - 28 Jul 2006 02:28 GMT
Hi,

If the table has fields like:

f1, dateStamp

then

SELECT a.f1, a.dateStamp As thisDateStamp,
        b.dateStamp As PreviousDStampForF1,
        a.dateStamp-b.dateStamp as Gap

FROM (myTable As a LEFT JOIN myTable As b ON a.f1=b.f1 AND
a.dateStamp>b.dateStamp)
               LEFT JOIN myTable As c ON a.f1=c.f1 AND a.dateStamp >
c.dateStamp

GROUP BY a.f1, a.dateStamp, b.dateStamp

HAVING b.dateStamp = MAX(c.dateStamp)

should do.

Hoping it may help,
Vanderghast, Access MVP

> Hello,
>
[quoted text clipped - 5 lines]
> week
> or 2 weeks (through evaluating the next record)?
David - 28 Jul 2006 12:41 GMT
Thanks for your reply.  I'm thinking we're close, but still get one small
error...

I built a query to "produce" the datestamp (year + payweek) and am using
that in the query, resulting in:

SELECT A.mgrname,  A.DateStamp AS ThisDateStamp, B.DateStamp AS
ThatDateStamp, A.DateStamp -B.DateStamp As GAP
FROM (qryDateStamp As A LEFT JOIN qryDateStamp AS B ON A.mgrname = B.mgrname
and A.DateStamp > B.DateStamp)
LEFT JOIN qryDateStamp AS C ON A.mgrname = C.mgrname and A.DateStamp >
C.DateStamp
GROUP BY A.mgrname, A.DateStamp, B.DateStamp Having B.DateStamp =
MAX(C.DateStamp)

I get the error: You tried to execute a query that does not include the
specified expression 'B.DateStamp = MAX(C.DateStamp) as part of an aggregate
function.

Not being an SQL guru, and not being able to view this in design mode (due
to Access not liking the ">" sign in the join), I'm slightly stuck.

Thanks.
Signature

David

> Hi,
>
[quoted text clipped - 31 lines]
> > week
> > or 2 weeks (through evaluating the next record)?
David - 28 Jul 2006 14:25 GMT
One more bit of info Michel,

I now get a different error message:Data Type Mismatch  In Criteria
Expression.

The query is:

SELECT A.mgrname, A.DateStamp AS ThisDateStamp, B.DateStamp AS
ThatDateStamp, DateDiff("w",A.DateStamp, B.DateStamp) AS GAP
FROM (qryDateStamp AS A LEFT JOIN qryDateStamp AS B ON (A.mgrname =
B.mgrname) AND (A.DateStamp > B.DateStamp)) LEFT JOIN qryDateStamp AS C ON
(A.mgrname = C.mgrname) AND (A.DateStamp > C.DateStamp)
GROUP BY A.mgrname, A.DateStamp, B.DateStamp
HAVING (((First(B.DateStamp))=Max([C].[DateStamp])));

The only difference between this, and the one I emailed on earlier, is the
field DateStamp is "created" in qryDateStamp as a SHORT DATE field, through
the syntax:  CDate(Left([paydate],2) & "-" & Mid([paydate],3,3) & "-" &
Right([paydate],2)).  Paydate is a field with the format ddmmmyy (i.e.
03Mar06).

If I run the query as noted below, I get no error message, but you can see
the JOIN statement has a.datestamp = b and c.datestamp vs. a.datestamp > b
and c.datestamp...

SELECT A.mgrname, A.DateStamp AS ThisDateStamp, B.DateStamp AS
ThatDateStamp, DateDiff("w",A.DateStamp, B.DateStamp) AS GAP
FROM (qryDateStamp AS A LEFT JOIN qryDateStamp AS B ON (A.mgrname =
B.mgrname) AND (A.DateStamp = B.DateStamp)) LEFT JOIN qryDateStamp AS C ON
(A.mgrname = C.mgrname) AND (A.DateStamp = C.DateStamp)
GROUP BY A.mgrname, A.DateStamp, B.DateStamp
HAVING (((First(B.DateStamp))=Max([C].[DateStamp])));

Any ideas why I get this error message?
Signature

David

> Hi,
>
[quoted text clipped - 31 lines]
> > week
> > or 2 weeks (through evaluating the next record)?
Michel Walsh - 29 Jul 2006 13:38 GMT
Hi,

If you have a text field compared to a numerical (or date_time) field, that
can be a problem, indeed. It is generally easier and faster to WORK on
native numerical data and to CONVERT / FORMAT  only at the presentation
stage.

You can keep the grid edition capability, if you are ready to loose the LEFT
JOIN (which we don't really need, at least, not for the bulk of the
debugging): transfer the content of the ON clauses into a WHERE clause, and
replace LEFT JOIN with a coma:

SELECT A.mgrname, A.DateStamp AS ThisDateStamp, B.DateStamp AS
ThatDateStamp, DateDiff("w",A.DateStamp, B.DateStamp) AS GAP
FROM     qryDateStamp AS A , qryDateStamp AS B , qryDateStamp AS C

WHERE (A.mgrname = B.mgrname) AND (A.DateStamp > B.DateStamp) AND
(A.mgrname = C.mgrname) AND (A.DateStamp > C.DateStamp)

GROUP BY A.mgrname, A.DateStamp, B.DateStamp
HAVING ((((B.DateStamp))=Max([C].[DateStamp])));

should be 'workable' in the query designer. It makes the WHERE clause more
messy than the individual ON clauses. You can also make a mix of all this,
putting back the =  parts as join, and leaving just the inequalities in the
WHERE clause.

Sure, some records will be missing, such as those where there is not records
in B such that

       A.DateStamp > B.DateStamp

but we were loosing those records, anyhow, even using the LEFT JOIN, with
our actual HAVING clause.

Note that since B.DateStamp is in the GROUP BY, we don't need to aggregate
it, with FIRST or otherwise, in the HAVING clause.

Hoping it may help,
Vanderghast, Access MVP

> One more bit of info Michel,
>
[quoted text clipped - 70 lines]
>> > week
>> > or 2 weeks (through evaluating the next record)?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.