?
Solved

MS Access Top 1 based on Date

Posted on 2014-09-09
20
Medium Priority
?
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
20 Comments
 
LVL 25

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 46

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 51

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 46

Accepted Solution

by:
aikimark earned 2000 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 46

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 46

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
 
LVL 46

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 46

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 46

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 46

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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