I'm using Access 2000, and I don't know a lot about Access.
My data has fields that hold a code for a time period. For instance, one time period might be '2006010' and that refers to a time period in 2006. These time periods sort as text correctly (so 20060010 is always before 20060011), but numbers may be skipped (so there may be 20060012 and 20060014 but no 20060013). I didn't make this database, so I have to use that data as-is.
What I want to do is get the sum of all the data (data is a separate field) from the previous 4 periods for each period.
For example if I have: -
20060010 1
-
20060011 2
-
20060012 4
-
20060014 8
-
20060015 16
-
20060016 32
-
20060017 64
-
in the original table I want the result to be: -
20060010 0 (or NULL)
-
20060011 1
-
20060012 3
-
20060014 7
-
20060015 15
-
20060016 31
-
20060017 62 (notice that it does not add the 1, it only sums 2,4,8,16,32)
-
The real data is not in a pattern, of course. What would be a good query to do this?
I am able to do this in another database with the following query: -
SELECT DISTINCT
-
i AS i1,
-
(SELECT
-
SUM(j)
-
FROM
-
(SELECT DISTINCT
-
i,
-
j
-
FROM
-
t
-
WHERE
-
i < t1.i
-
ORDER BY
-
i DESC
-
LIMIT 5
-
) t2
-
) AS i2 FROM
-
t AS t1;
-
But in Access 2000 (after I modify the query to use TOP rather than LIMIT) it does not recognize "t1.i" in the inner inner query.
Any help is appreciated. Also, if someone has a good link to an Access 2000 reference that would help me; for some reason I am having problems finding out which functions are even available to me to make my queries.
Thanks!
Jeff
4 1369 NeoPa 32,579
Recognized Expert Moderator MVP
Jeff,
I've been mulling over this for a while now and what you're trying to do is (as far as I'm aware) unsupported in any SQL engine.
Believe me when I give you kudos for the concepts your trying to use but, because in your SQL the engine would need to reprocess the embedded subqueries for each record, this cannot work as is. Your trying to access t1 (defined at the outer level) from within a subquery is a clue and cannot work.
If you give me a short while longer I will try to provide a query (SQL) which can work - it will be based on Domain Aggregate functions.
-Adrian.
NeoPa 32,579
Recognized Expert Moderator MVP
This is a little embarrasing but it appears that, for various reasons, that is not possible either.
1. Domain Aggregate functions cannot work from a SQL string as a domain.
2. There is no way to specify SELECT predicates (DISTINCT; TOP ?; etc) in Domain Aggregate functions.
3. Domain Aggregate functions can work with Tables or QueryDefs - but not QueryDefs that take parameters.
I'm not sure I could have found a solution with a parameter query anyway, but the other two were certainly show-stoppers.
This problem doesn't appear to have a solution based on simple (or otherwise) SQL, but it can be done with RecordSets in VBA code.
Mary should be along shortly with some pointers (Knowing Mary probably Bells & Whistles too) to help.
ADezii 8,834
Recognized Expert Expert
I'm using Access 2000, and I don't know a lot about Access.
My data has fields that hold a code for a time period. For instance, one time period might be '2006010' and that refers to a time period in 2006. These time periods sort as text correctly (so 20060010 is always before 20060011), but numbers may be skipped (so there may be 20060012 and 20060014 but no 20060013). I didn't make this database, so I have to use that data as-is.
What I want to do is get the sum of all the data (data is a separate field) from the previous 4 periods for each period.
For example if I have: -
20060010 1
-
20060011 2
-
20060012 4
-
20060014 8
-
20060015 16
-
20060016 32
-
20060017 64
-
in the original table I want the result to be: -
20060010 0 (or NULL)
-
20060011 1
-
20060012 3
-
20060014 7
-
20060015 15
-
20060016 31
-
20060017 62 (notice that it does not add the 1, it only sums 2,4,8,16,32)
-
The real data is not in a pattern, of course. What would be a good query to do this?
I am able to do this in another database with the following query: -
SELECT DISTINCT
-
i AS i1,
-
(SELECT
-
SUM(j)
-
FROM
-
(SELECT DISTINCT
-
i,
-
j
-
FROM
-
t
-
WHERE
-
i < t1.i
-
ORDER BY
-
i DESC
-
LIMIT 5
-
) t2
-
) AS i2 FROM
-
t AS t1;
-
But in Access 2000 (after I modify the query to use TOP rather than LIMIT) it does not recognize "t1.i" in the inner inner query.
Any help is appreciated. Also, if someone has a good link to an Access 2000 reference that would help me; for some reason I am having problems finding out which functions are even available to me to make my queries.
Thanks!
Jeff
I managed to duplicate the results using reverse logic and a Query with a Calculated Field but did not have the time to refine the code - it is very crude and preliminary. Create a SELECT Query as follows with comparable Field Names:
[Time_Period].....[Data].......Data_Sum : fSumData([Data])
Ascending...... ............... .....(Calculate d Field) - 'Global Array to store persisted Data Values
-
Public DataArray(1000) As Long 'Arbitrary number
- 'Crude Function called from the Calculated Field
-
Public Function fSumData(MyData As Long)
-
Static RecordNumber As Long
-
-
RecordNumber = RecordNumber + 1
-
-
DataArray(RecordNumber) = MyData
-
-
Select Case RecordNumber
-
Case 1
-
fSumData = 0
-
Case 2
-
fSumData = DataArray(1)
-
Case 3
-
fSumData = DataArray(1) + DataArray(2)
-
Case 4
-
fSumData = DataArray(1) + DataArray(2) + DataArray(3)
-
Case 5
-
fSumData = DataArray(1) + DataArray(2) + DataArray(3) + DataArray(4)
-
Case 6
-
fSumData = DataArray(1) + DataArray(2) + DataArray(3) + DataArray(4) + DataArray(5)
-
Case 7
-
fSumData = DataArray(2) + DataArray(3) + DataArray(4) + DataArray(5) + DataArray(6)
-
Case Else
-
fSumData = MyData
-
End Select
-
End Function
NOTE: The Public Array and Static Record Counter must somehow be Reset prior to Query execution. Got the idea - sorry I didn't have time to refine the coding.
MMcCarthy 14,534
Recognized Expert Moderator MVP
This problem doesn't appear to have a solution based on simple (or otherwise) SQL, but it can be done with RecordSets in VBA code.
Mary should be along shortly with some pointers (Knowing Mary probably Bells & Whistles too) to help.
Sorry for the delay Jeff.
My solution would involve the use of recordsets in vba. You would need to create a table to hold the results. (tblResults) -
Function Period Totals()
-
Dim db as Database
-
Dim rs1 as DAO.Recordset
-
Dim rs2 as DAO.Recordset
-
Dim rs3 as DAO.Recordset
-
Dim iDate As String
-
Dim jSum As Integer
-
Dim i as Integer
-
-
Set db = CurrentDB
-
Set rs1 = db.OpenRecordset("SELECT DISTINCT i FROM t ORDER BY i DESC;")
-
Set rs2 = db.OpenRecordset("SELECT i, j FROM t ORDER BY i DESC;")
-
Set rs3 = db.OpenRecordset("tblResults")
-
-
rs1.MoveFirst
-
Do Until rs1.EOF
-
rs2.MoveFirst
-
jSum = 0
-
rs2.FindFirst "i = " & rs1!i
-
For i = 1 To 5
-
rs2.MoveNext
-
jSum = jSum + rs2!j
-
i = i + 1
-
Next i
-
rs3.Add
-
rs3!i = rs1!i
-
rs3!j = jSum
-
rs3.Update
-
rs1.MoveNext
-
Loop
-
-
rs1.Close
-
rs2.Close
-
rs3.Close
-
Set rs1 = Nothing
-
Set rs2 = Nothing
-
Set rs3 = Nothing
-
Set db = Nothing
-
-
End Function
-
This will give you the sum of the previous five periods for each date. If you need something different. Let me know.
Mary
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: majsen |
last post by:
Hi, I have problem running this query. It will time out for me...
My database are small just about 200 members.
I have a site for swaping appartments (rental). my query should look
for match
in a triangle. Like this member A -> B->C
A give his appartment to B. B gives his appartment to C and finally
C gives his appartment to A
Soo my query looks for matching parameters like rooms, location, size
|
by: Együd Csaba |
last post by:
Hi All,
how can I improve the query performance in the following situation:
I have a big (4.5+ million rows) table. One query takes approx. 9 sec to
finish resulting ~10000 rows. But if I run simultaneously 4 similar queries
it takes nearly 5 minutes instead of 4 times 9 seconds or something near of
that.
here is a sample query:
select mertido, fomeazon, ertektipus, mertertek from t_me30 where fomeazon
in (select distinct fomeazon...
|
by: John Ortt |
last post by:
> I have a table of dates in ascending order but with varying intervals. I
> would like to create a query to pull out the date (in field 1) and then
pull
> the date from the subsequent record (and store it in field 2).
>
> I would like to run a query that returns all the data in the table plus
the
> record number, in a similar sense to the getuser() command to get the
User's
> Identity, I would like a GetRecord() command.
|
by: Ekqvist Marko |
last post by:
Hi,
I have one Access database table including questions and answers. Now
I need to give answer id automatically to questionID column. But I
don't know how it is best (fastest) to do?
table before
rowID answID qryrow questionID datafield
1591 12 06e 06e 06e question
1593 12 06f 06f 06f question
1594 12 answer to the question 06f
|
by: serge |
last post by:
How can I run a single SP by asking multiple sales question either
by using the logical operator AND for all the questions; or using
the logical operator OR for all the questions. So it's always
either AND or OR but never mixed together.
We can use Northwind database for my question, it is very similar
to the structure of the problem on the database I am working on.
IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL
DROP TABLE REPORT_SELECTION
| |
by: jjturon |
last post by:
Can anyone help me??
I am trying to pass a Select Query variable to a table using Dlookup
and return the value to same select query but to another field.
Ex.
SalesManID SalesManName
AT Alan Time
|
by: mmitchell_houston |
last post by:
I'm working on a .NET project and I need a single query to return a
result set from three related tables in Access 2003, and I'm having
trouble getting the results I want.
The details:
Question ------------
QuestionID
QuestionText
Question_MediaTypeID
|
by: Stan |
last post by:
I am working with Access 2003 on a computer running XP. I am new at
using Access. I have a Db with a date field stored as mm/dd/yyyy. I
need a Query that will prompt for the month, ie. 6 for June, and will
return all records in that month.
|
by: Richard Hollenbeck |
last post by:
I am very sorry about the (almost) re-post, but you will see that my first
question wasn't very clear; I have another question I posted this morning
called, "in DAO: Run time error 3061 Too few parameters...."
I have read many articles on the web about how to make a dynamic report
based on a cross-tab query. But for some reason mine never works right.
First, my saved query's criteria is the data in an open form's combo box.
So the...
|
by: ARC |
last post by:
Hello all,
So I'm knee deep in this import utility program, and am coming up with all
sorts of "gotcha's!".
1st off. On a "Find Duplicates Query", does anyone have a good solution for
renaming the duplicate records? My thinking was to take the results of the
duplicate query, and somehow have it number each line where there is a
duplicate (tried a groups query, but "count" won't work), then do an update
query to change the duplicate to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |