473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Group and sum transactions for last year in which a transaction exists

I have a table called Transactions with 3 fields: ID, Date, and Amount. Each
ID can have multiple transactions in one particular year. An ID might not
have had any transactions in recent years.

For each ID I would like to sum all the transactions for the most recent
year in which one or more transactions have occurred.

Thanks for any help and for all previous help.
Nov 13 '05 #1
6 2781
Create a query showing the data you want ie all records where:
TransactionDate >= DateAdd("m", -12, Date)
Use that query to sum the Amount.

Incidently, I trust that you have not really used 'Date' for the field name
in your table!
--
Bob Darlington
Brisbane
"Terri" <Te***@spamaway.com> wrote in message
news:ci**********@reader2.nmix.net...
I have a table called Transactions with 3 fields: ID, Date, and Amount. Each ID can have multiple transactions in one particular year. An ID might not
have had any transactions in recent years.

For each ID I would like to sum all the transactions for the most recent
year in which one or more transactions have occurred.

Thanks for any help and for all previous help.

Nov 13 '05 #2

"Bob Darlington" <bo*@dpcmanAX.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
Create a query showing the data you want ie all records where:
TransactionDate >= DateAdd("m", -12, Date)
Use that query to sum the Amount.

Incidently, I trust that you have not really used 'Date' for the field name in your table!


Thanks. Unfortunately the field is named date which I know is a reserved
word. I didn't design the database and the field name can't be changed.

I built a preliminary query (q1) and aliased the field as TransactionDate.
My second query (q2) is now:

SELECT q1.ID, q1.TransactionDate
FROM q1
WHERE (((q1.TransactionDate)>=DateAdd("m",-12,[TransactionDate])))
ORDER BY q1.ID;

The second query is returning the same number of records as the first so I'm
not sure if the DateAdd function is what I need. Given the following
TransactionDates in the first query:

4/12/2003
1/1/2003
12/23/2002
5/1/2001

I would only want to return:
4/12/2003
1/1/2003
which is all transactions in the most recent year for which there is a
transaction.

Thanks
Nov 13 '05 #3
Hi Terri,

I guess from your messages you're talking about calendar years (e.g. as
opposed to financial). If that's the case, you can do what you want by
using the Year() function and a series of three queries.

(1) Get sum ID Amount Sum for every year in the table:

SELECT Transactions.ID, Year([TransactionDate]) AS TransactionYear,
Sum(Transactions.Amount) AS YearAmount
FROM Transactions
GROUP BY Transactions.ID, Year([TransactionDate]);

(2) Get the latest year for each ID from the query above:

SELECT qry1_year_sums_all.ID, Max(qry1_year_sums_all.TransactionYear) AS
MaxTransactionYear
FROM qry1_year_sums_all
GROUP BY qry1_year_sums_all.ID;

(3) Join the above queries to get the yearly sums for the latest years
only:

SELECT qry1_year_sums_all.*
FROM qry2_max_year INNER JOIN qry1_year_sums_all ON
(qry1_year_sums_all.TransactionYear = qry2_max_year.MaxTransactionYear)
AND (qry2_max_year.ID = qry1_year_sums_all.ID);

Let me know if you have any trouble with these queries.

Cheers,
------------
LoopyNZ
------------

=====================
Original Message:

I have a table called Transactions with 3 fields: ID, Date, and Amount.
Each
ID can have multiple transactions in one particular year. An ID might
not
have had any transactions in recent years.

For each ID I would like to sum all the transactions for the most recent
year in which one or more transactions have occurred.

Thanks for any help and for all previous help.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4
Terri,
Try using the actual field name ([date]) instead of the alias in the
DateAdd. It should work OK.

--
Bob Darlington
Brisbane
"Terri" <Te***@spamaway.com> wrote in message
news:ci*********@reader2.nmix.net...

"Bob Darlington" <bo*@dpcmanAX.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
Create a query showing the data you want ie all records where:
TransactionDate >= DateAdd("m", -12, Date)
Use that query to sum the Amount.

Incidently, I trust that you have not really used 'Date' for the field name
in your table!


Thanks. Unfortunately the field is named date which I know is a reserved
word. I didn't design the database and the field name can't be changed.

I built a preliminary query (q1) and aliased the field as TransactionDate.
My second query (q2) is now:

SELECT q1.ID, q1.TransactionDate
FROM q1
WHERE (((q1.TransactionDate)>=DateAdd("m",-12,[TransactionDate])))
ORDER BY q1.ID;

The second query is returning the same number of records as the first so

I'm not sure if the DateAdd function is what I need. Given the following
TransactionDates in the first query:

4/12/2003
1/1/2003
12/23/2002
5/1/2001

I would only want to return:
4/12/2003
1/1/2003
which is all transactions in the most recent year for which there is a
transaction.

Thanks

Nov 13 '05 #5
"Bob Darlington" <bo*@dpcmanAX.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
Terri,
Try using the actual field name ([date]) instead of the alias in the
DateAdd. It should work OK.

--
Bob Darlington
Brisbane
"Terri" <Te***@spamaway.com> wrote in message
news:ci*********@reader2.nmix.net...

"Bob Darlington" <bo*@dpcmanAX.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
Create a query showing the data you want ie all records where:
TransactionDate >= DateAdd("m", -12, Date)
Use that query to sum the Amount.
=DateAdd("m",-12,[Date]) works now that I put the brackets around the field

but the query returns the same amount of records with or without the
criteria.

SELECT transactions.Date
FROM transactions;

same amount of records as

SELECT transactions.Date
FROM transactions
WHERE (((transactions.Date)>=DateAdd("m",-12,[Date])));

Do I need to sort or group?
Nov 13 '05 #6
OK. Now I'll open my eyes and try again.
Try DateAdd("m",-12,Date())
All records will have been after DateAdd("m",-12,[Date]) .
You want 12 months before today (Date()), not 12 monhts before the first
transaction date.

--
Bob Darlington
Brisbane
"Terri" <Te***@spamaway.com> wrote in message
news:ci**********@reader2.nmix.net...
"Bob Darlington" <bo*@dpcmanAX.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
Terri,
Try using the actual field name ([date]) instead of the alias in the
DateAdd. It should work OK.

--
Bob Darlington
Brisbane
"Terri" <Te***@spamaway.com> wrote in message
news:ci*********@reader2.nmix.net...

"Bob Darlington" <bo*@dpcmanAX.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
> Create a query showing the data you want ie all records where:
> TransactionDate >= DateAdd("m", -12, Date)
> Use that query to sum the Amount.
=DateAdd("m",-12,[Date]) works now that I put the brackets around the

field but the query returns the same amount of records with or without the
criteria.

SELECT transactions.Date
FROM transactions;

same amount of records as

SELECT transactions.Date
FROM transactions
WHERE (((transactions.Date)>=DateAdd("m",-12,[Date])));

Do I need to sort or group?

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John | last post by:
If anyone can help me out with a good way to do this in javascript I would greatly appreciate it. I need to compute three dates in javascript - all of which relate to a given date, say todays...
0
by: Newbillian | last post by:
After converting Access 97 databases to 2003 this error msg shows up after a line about not being able to save the record because a related record is needed in another table. The complete msg is: ...
1
by: Dan Leeder | last post by:
stroccur = DCount("", "empnotes", " = " & Chr(34) & Me.Rpt_Card_Type & Chr(34) & " And = " & Me.SSN & " And #" & & "# > " & DateSerial(Year(Me.datetime) - 1, Month(Me.datetime),...
9
by: John Sidney-Woollett | last post by:
Is it possible to use the dblink and dblink_exec features from inside pl/pgsql functions to mimic the behaviour of nested transactions by calling another function or executing some SQL via the...
1
by: Frank Bishop | last post by:
I have been spoiled by some report writing tools that have intrinsic functions like Last Year Month-to-date. I'm looking for a way to emulate this in SQL Server now with my fields that are...
5
by: rjfjohnson | last post by:
Hey, Today is Thursday 16-Feb-06. The same thursday last year is 17-Feb-05. Because I am comparing daily sales between years, I need to know the date of the same weekdayname as last year, so...
6
by: phforum | last post by:
If user input the date is 2006-07-01. How to convert it to last year 2005-07-01? Thanks
0
by: pratapmysql | last post by:
Hello All, I am trying to get same day (name) of week of last year in this case date of day is not constraint eg. if december 2007-12-19 day is wednesday ,then last year 2006 wednesday for same is...
7
by: sasimca007 | last post by:
Hello friends, What my doubt is, a page is already designed in that page a table exists, and i am doing a script like a radio button exixts and when click that radio button...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.