Solved

MS Access Top 1 based on Date

Posted on 2014-09-09
20
165 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

828 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