Add Previous record to Current record

B

broogle

Date Amount Cumm_Amount
01/01/06 1
12/01/05 2
24/01/04 3
24/01/06 4


The result I want:

-Sort by date and in cumm_amount
-first data in cumm_amount taken from first data in Amount field.
-Second, third, and so on taken from (Amount + Amount in next record)

Is it possible to create a query for this? Thanks


Result I expect:

Date Amount Cumm_Amount
24/01/04 3 3
12/01/05 2 5
01/01/06 1 3
24/01/06 4 5


Thanks
 
G

Guest

You will need to create a report and set the Running Sum property of the
amount field to Yes. However you may need to have something to identify which
records need to be added and then Group on that field (unless you already
have this) Access DOES NOT work like Excel so you cannot assume that records
are maintained tin the order in whihc they are entered.

HTH - Sheila
 
J

Jamie Collins

Sheila said:
You will need to create a report

You are right that this is probably best done in a report but it *can*
also be achieved in a query...
you may need to have something to identify which
records need to be added and then Group on that field (unless you already
have this) Access DOES NOT work like Excel so you cannot assume that records
are maintained tin the order in whihc they are entered.

In this case the OP has stated that the 'Date' column provides the
order. There are missing aspects of the spec, though e.g. what to do
when there are ties for 'previous date' values? I'll assume the date
values are unique.

I didn't post this yesterday because I was ashamed of the fact I had to
use the table *three* times. Perhaps someone can do better...?

SELECT DT1.blah_date, DT1.blah_amount,
DT1.blah_previous_date,
T3.blah_amount AS blah_previous_amount,
DT1.blah_amount +
IIF(T3.blah_amount IS NULL, 0, T3.blah_amount)
AS blah_cumm_amount
FROM
(
SELECT T2.blah_date, T2.blah_amount,
MAX(T1.blah_date) AS blah_previous_date
FROM Blah AS T1
RIGHT JOIN Blah AS T2
ON T1.blah_date < T2.blah_date
GROUP BY T2.blah_date, T2.blah_amount
) AS DT1
LEFT JOIN Blah AS T3
ON DT1.blah_previous_date = T3.blah_date
ORDER BY DT1.blah_date;

As usual, here's the VBA to reproduce the scenario, test data and
results:

Sub blahs()
Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat
.Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\DropMe.mdb"
With .ActiveConnection

' Create test table
.Execute _
"CREATE TABLE Blah ( blah_date DATETIME NOT" & _
" NULL UNIQUE, blah_amount INTEGER NOT NULL)"

' Create test data
.Execute _
"INSERT INTO Blah (blah_date, blah_amount)" & _
" VALUES (#2006-01-01 00:00:00#, 1);"
.Execute _
"INSERT INTO Blah (blah_date, blah_amount)" & _
" VALUES ('2005-01-12 00:00:00', 2);"
.Execute _
"INSERT INTO Blah (blah_date, blah_amount)" & _
" VALUES (#2004-01-24 00:00:00#, 3);"
.Execute _
"INSERT INTO Blah (blah_date, blah_amount)" & _
" VALUES (#2006-01-24 00:00:00#, 4);"

Dim rs
Set rs = .Execute( _
"SELECT DT1.blah_date, DT1.blah_amount, DT1.blah_previous_date," &
_
" T3.blah_amount AS blah_previous_amount," & _
" DT1.blah_amount + IIF(T3.blah_amount IS" & _
" NULL, 0, T3.blah_amount) AS blah_cumm_amount" & _
" FROM (SELECT T2.blah_date, T2.blah_amount," & _
" MAX(T1.blah_date) AS blah_previous_date" & _
" FROM Blah AS T1 RIGHT JOIN Blah AS T2 ON" & _
" T1.blah_date < T2.blah_date GROUP BY T2.blah_date," & _
" T2.blah_amount) AS DT1 LEFT JOIN Blah AS" & _
" T3 ON DT1.blah_previous_date = T3.blah_date" & _
" ORDER BY DT1.blah_date")
MsgBox rs.GetString(2, , , , "(null)" & vbTab)
rs.Close
End With
Set .ActiveConnection = Nothing
End With
End Sub

Jamie.

--
 
G

Guest

I'm sure you're right Jamie - bit of a sledgehammer to crack a nut though I
would have thought!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top