Solved

MS Access Top 1 based on Date

Posted on 2014-09-09
20
166 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 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 50

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

738 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