MS Access Top 1 based on Date

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
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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
WHERE (A.sample_date Between #4/1/2013# And #5/31/2013#);

Open in new window

if qryA.fds is null, will there be a non-null qryA.sample_date for that row?
Gustav BrockCIOCommented:
How about just pulling that fds from the dupe:

      (select top 1
          qryA As Dupe
          Dupe.sample_date <= qryA.sample_date
          Dupe.fds is not null
      order by
          Dupe.sample_date desc) AS

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sj_rodriguezAuthor Commented:
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.
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
WHERE (qryA.sample_date Between #4/1/2013# And #5/31/2013#);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sj_rodriguezAuthor Commented:
Same error.
Do you have any Null dates?
sj_rodriguezAuthor Commented:
Can you post some sample data?

What version of Access are you using?
sj_rodriguezAuthor Commented:
MS Access 2010.

I have attached an excel file with 2 months of data.
What does qryA look like?
sj_rodriguezAuthor Commented:
The data I attached in excel is a sample of the data from qryA.  

columns for sample_date, flow, ph, fds, tkn, etc...
Do you have all the service patches applied to Access?
sj_rodriguezAuthor Commented:
I believe so... I will check.
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.
sj_rodriguezAuthor Commented:
Let me make my qryA into a table and try it too.
sj_rodriguezAuthor Commented:
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.
Please post the SQL.
sj_rodriguezAuthor Commented:
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.fds, qry_qual_3.tkn, qry_qual_3.no3_no2, qry_qual_3.bod5,, qry_qual_3.k,,,, 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;
sj_rodriguezAuthor Commented:
I appreciated all the help received from aikimark.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.