Solved

MS Access Top 1 based on Date

Posted on 2014-09-09
20
162 Views
Last Modified: 2014-09-10
I have a daily data set that has some data that is not daily - it might be weekly or bi-weekly or some other timeframe.  For data that is not daily, I need to use the most recent value in all days following until I hit a day with a new value.  I am trying to run the following query to carry a value from one day to the next if there isn't a value there for use in a daily calculation.   When I run the following, I get an "Invalid Argument to Function Error"

SELECT qryA.sample_date, qryA.flow, iif(qryA.fds is not null, qryA.fds,(SELECT TOP 1 Dupe.fds
     FROM qryA as Dupe
     WHERE Dupe.sample_date < qryA.sample_date AND Dupe.FDS Is Not Null
     ORDER BY Dupe.sample_date DESC)) AS fds_mod
FROM qryA
WHERE (qryA.sample_date Between #4/1/2013# And #5/31/2013#);

If I change the "WHERE Dupe.sample_date < qryA.sample_date" to "WHERE Dupe.sample_date < #5/16/2013#" it works.  I need to be able to run the select top 1 based on the current records date.
0
Comment
Question by:sj_rodriguez
20 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 40313416
What happens if you add an alias to the main query:
SELECT A.sample_date, A.flow, iif(A.fds is not null, A.fds,(SELECT TOP 1 Dupe.fds
     FROM qryA as Dupe
     WHERE Dupe.sample_date < A.sample_date AND Dupe.FDS Is Not Null
     ORDER BY Dupe.sample_date DESC)) AS fds_mod
FROM qryA A
WHERE (A.sample_date Between #4/1/2013# And #5/31/2013#);

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40313593
if qryA.fds is null, will there be a non-null qryA.sample_date for that row?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40313908
How about just pulling that fds from the dupe:

SELECT
    qryA.sample_date,
    qryA.flow,
      (select top 1
          Dupe.fds
      from
          qryA As Dupe
      where
          Dupe.sample_date <= qryA.sample_date
          and
          Dupe.fds is not null
      order by
          Dupe.sample_date desc) AS
    fds
FROM
    qryA;

/gustav
0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40314937
I tried adding an alias to the main query... got the same error.

The data set has a flow value for every day.  There is an fds value every week or so.  I need to carry the fds value forward each day until there is a new value.   (i.e., There is an fds value on 5/7/2013.  The next fds value is on 5/23/2013.  I need to have the 5/7 value returned for 5/8-5/22)

I tried pulling the fds value from the dupe query... got the same error.  If I change the where statement to have a hard coded date instead of looking at the main query row date, it works.  But then all the fds values returned are that one value and not based on the date of the row.
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40315011
Please try the following:
SELECT qryA.sample_date, qryA.flow, 
     iif(IsNull(qryA.fds), (SELECT TOP 1 Dupe.fds
     FROM qryA as Dupe
     WHERE Dupe.sample_date < qryA.sample_date AND Dupe.FDS Is Not Null 
     ORDER BY Dupe.sample_date DESC), qryA.fds) AS fds_mod
FROM qryA
WHERE (qryA.sample_date Between #4/1/2013# And #5/31/2013#);

Open in new window

0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40315024
Same error.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40315039
Do you have any Null dates?
0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40315041
Nope.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40315051
Can you post some sample data?

What version of Access are you using?
0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40315058
MS Access 2010.

I have attached an excel file with 2 months of data.
Sample-Data.xlsx
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Expert Comment

by:aikimark
ID: 40315097
What does qryA look like?
0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40315110
The data I attached in excel is a sample of the data from qryA.  

columns for sample_date, flow, ph, fds, tkn, etc...
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40315121
Do you have all the service patches applied to Access?
0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40315132
I believe so... I will check.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40315139
I imported the data as table qryA in Access2007 and run the SQL I posted.  No errors.  Got the following results:
sample_date	flow	fds_mod
4/1/2013	4.2	
4/2/2013	3.08	682
4/3/2013	2.01	682
4/4/2013	2.83	682
4/5/2013	2.03	682
4/6/2013	1.54	682
4/7/2013	0	682
4/8/2013	1.52	682
4/9/2013	2.78	669
4/10/2013	1.58	669
4/11/2013	3.18	669
4/12/2013	0.07	669
4/13/2013	0	669
4/14/2013	0	669
4/15/2013	2.19	669
4/16/2013	0.81	655
4/17/2013	1.05	655
4/18/2013	2.41	655
4/19/2013	3.04	655
4/20/2013	0.52	655
4/21/2013	0	655
4/22/2013	0.91	655
4/23/2013	0.59	655
4/24/2013	0.82	655
4/25/2013	0.83	655
4/26/2013	0.12	655
4/27/2013	0	655
4/28/2013	0	655
4/29/2013	0.73	655
4/30/2013	0.19	655
5/1/2013	0.7	655
5/2/2013	0.77	655
5/3/2013	0.23	655
5/4/2013	0	655
5/5/2013	0	655
5/6/2013	0.28	655
5/7/2013	1.08	672
5/8/2013	0.66	672
5/9/2013	0.9	672
5/10/2013	0.81	672
5/11/2013	0.48	672
5/12/2013	0.13	672
5/13/2013	0.29	672
5/14/2013	0.71	672
5/15/2013	0.52	672
5/16/2013	0.65	672
5/17/2013	0.4	672
5/18/2013	0.45	672
5/19/2013	0	672
5/20/2013	0.42	672
5/21/2013	0.15	672
5/22/2013	0.75	672
5/23/2013	1	685
5/24/2013	0.52	685
5/25/2013	0.95	685
5/26/2013	0.11	685
5/27/2013	0.98	685
5/28/2013	1.85	685
5/29/2013	1.25	685
5/30/2013	0.31	685
5/31/2013	1.48	685

Open in new window

Checking against the table, this seems correct.
0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40315141
Let me make my qryA into a table and try it too.
0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40315149
It works for me too from the table.  Let me re-look at my qryA query - there has to be something in there causing the problem.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40315150
Please post the SQL.
0
 
LVL 1

Author Comment

by:sj_rodriguez
ID: 40315182
I found the issue...  As I was getting the SQL for you, I found I had an odd join/grouping thing going on to create my qryA.  qryA combines a flow table and a quality table to get a combined dataset.  I have fixed that and then the query using the top 1 works.

Corrected SQL for qryA:

SELECT qry_qual_2.sample_date, qry_qual_2.flow, qry_qual_3.ph, qry_qual_3.fds, qry_qual_3.tkn, qry_qual_3.no3_no2, qry_qual_3.bod5, qry_qual_3.ca, qry_qual_3.k, qry_qual_3.mg, qry_qual_3.na, qry_qual_3.cl, qry_qual_3.so4, qry_qual_3.hco3, qry_qual_3.total_p
FROM qry_qual_2 LEFT JOIN qry_qual_3 ON (qry_qual_2.ww_id = qry_qual_3.ww_id) AND (qry_qual_2.sample_date = qry_qual_3.sample_date)
ORDER BY qry_qual_2.sample_date;
0
 
LVL 1

Author Closing Comment

by:sj_rodriguez
ID: 40315183
I appreciated all the help received from aikimark.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now