If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > easy SQL question: running total

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 12-10-03, 03:53
Virgo-Libra Virgo-Libra is offline
Registered User
 
Join Date: Nov 2003
Location: Riga, Latvia
Posts: 36
Quote:
Originally posted by izyrider
i'm trying my theta join right now.

probably i'll muddle through, but where did t2 come from?

izy
from runningtotal t1, runningtotal t2

As I understood, Rudy suggests using the same table twice with different aliases - as "t1" for calculation and as "t2" to have a sorted column to compare with
__________________
Replies based on my configuration: Windows XP / Microsoft Office Access 2003 unless specified otherwise.
Reply With Quote
  #17 (permalink)  
Old 12-10-03, 04:39
Apel Apel is offline
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 228
Quote:
Originally posted by izyrider
query2:
SELECT Query1.Month, CalcRT([SumOfaNum]) AS RunningTotal
FROM Query1
ORDER BY Query1.Month
A problem could occour here because of Access' assumption functions to be deterministic and not having any side effects. If you'd have duplicate values on SumOfaNum Access would likely optimize away the function call and use the cached result instead. But your running sum wouldn't be updated then.
A workaround would be to additionally pass a truely unique value to "cheat" the optimizer.
Reply With Quote
  #18 (permalink)  
Old 12-10-03, 06:24
izyrider izyrider is offline
Cavalier King Charles
 
Join Date: Dec 2002
Location: Préverenges, Switzerland
Posts: 3,731
problem with the theta join!!!!

...and back to the fabulous theta join.

i have a problem

playing with A's stored queries 'cos it's easier for experiments, the closest i can get to rudy's SQL is:

SELECT t1.Month, t1.aNum, sum(t2.aNum) AS runTot
FROM aTable AS t1, aTable AS t2
WHERE t1.Month >= t2.Month
GROUP BY t1.Month, t1.aNum;
...to me this looks pretty much equivalent to rudy's statement.

aTable:
myID, Month, aNum
1,1,1
2,1,1
3,1,1
4,1,1
5,2,1
6,2,1
..........etc so that each of months 1...6 have four rows with aNum=1

expected running total is:
4, 8, 12, 16, 20, 24 (delta = 4)

what i actually get returned is:
16, 32, 48, 64, 80, 96 (delta = 16) (??? sixteen ???? where's that from?)

why ">" in WHERE t1.Month >= t2.Month (it obviously makes sense, because "<" simply reverses the return 96, 80, 64....)

izy
__________________
currently using SS 2008R2
Reply With Quote
  #19 (permalink)  
Old 12-10-03, 06:28
izyrider izyrider is offline
Cavalier King Charles
 
Join Date: Dec 2002
Location: Préverenges, Switzerland
Posts: 3,731
...and switching from ">=" to ">" changes nothing. izy
__________________
currently using SS 2008R2
Reply With Quote
  #20 (permalink)  
Old 12-10-03, 07:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 20,000
izy, the reason your example returns "blown up" results is because in your join, each row is joining with 4 times as many rows as normal, because you are joining on the wrong column

each row should join with all "less than or equal" rows only once

it's a running total query, not a query to produce intermediate sums by month

it has to work on a primary (i.e. unique) key

try this instead --
Code:
select t1.myId, t1.Month, t1.aNum, sum(t2.aNum) AS runTot
from aTable as t1, aTable as t2
where t1.myId >= t2.myId
group by t1.myId, t1.Month, t1.aNum
this produces

myId Month aNum runTot
1 1 1 1
2 1 1 2
3 1 1 3
4 1 1 4
5 2 1 5
6 2 1 6
7 2 1 7
8 2 1 8
9 3 1 9
10 3 1 10
11 3 1 11
12 3 1 12
13 4 1 13
14 4 1 14
15 4 1 15
16 4 1 16
17 5 1 17
18 5 1 18
19 5 1 19
20 5 1 20
21 6 1 21
22 6 1 22
23 6 1 23
24 6 1 24

rudy

Edit: changed table names back to izy's example -- i prefer to set up meaningful names when creating test cases, and i forget to change back after pasting)

Last edited by r937; 12-10-03 at 07:52.
Reply With Quote
  #21 (permalink)  
Old 12-10-03, 07:03
Virgo-Libra Virgo-Libra is offline
Registered User
 
Join Date: Nov 2003
Location: Riga, Latvia
Posts: 36
Quote:
Originally posted by izyrider
...and switching from ">=" to ">" changes nothing. izy
For Rudy's statement to work, it requires a unique id column to be used, otherwise access will by default assume the "full join" statement and cross-link all corresponding rows (4 rows x 4 rows = that's where 16 comes from).

However, making two queries like

Query queSubtotals
SELECT t1.Month, Sum(t1.Num) AS SubTotal
FROM atable AS t1 INNER JOIN atable AS t2 ON t1.id = t2.id
WHERE (((t1.Month)>=[t2].[month]))
GROUP BY t1.Month;

Query queRunningTotal:
SELECT qT1.Month, Sum(qT1.SubTotal) AS RunningTotal
FROM queSubtotals AS qT2, queSubtotals AS qT1
WHERE (((qT1.Month)>=[qT2].[month]))
GROUP BY qT1.Month;

might be a solution - the second one gives correct reqults.
__________________
Replies based on my configuration: Windows XP / Microsoft Office Access 2003 unless specified otherwise.

Last edited by Virgo-Libra; 12-10-03 at 07:07.
Reply With Quote
  #22 (permalink)  
Old 12-10-03, 07:04
Virgo-Libra Virgo-Libra is offline
Registered User
 
Join Date: Nov 2003
Location: Riga, Latvia
Posts: 36
Ooops, posted simultaneously... Well, there you go
__________________
Replies based on my configuration: Windows XP / Microsoft Office Access 2003 unless specified otherwise.
Reply With Quote
  #23 (permalink)  
Old 12-10-03, 07:31
izyrider izyrider is offline
Cavalier King Charles
 
Join Date: Dec 2002
Location: Préverenges, Switzerland
Posts: 3,731
got it

thanks, izy
__________________
currently using SS 2008R2
Reply With Quote
  #24 (permalink)  
Old 02-14-13, 16:53
umchem umchem is offline
Registered User
 
Join Date: Jan 2013
Posts: 9
Hello All,

This issue is really the bain of my existance. I'm glad I found this thread, thugh hope someone could helop follow-up on it a bit. I'm trying the exact same thing with R937 (Rudy)'s code, and am getting the following error:

You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field.

I am a total noob and have no idea how to revise the code below, can anyone help out?
Code:
Expr1: 
(SELECT T1.StudyYears, T1.Balance2, sum(T2.Balance2) 
FROM FP_Default as T1, FP_Default as T2 
WHERE (T1.StudyYears >= T2.StudyYears) 
GROUP by T1.StudyYears, T1.Balance2)
Thanks again!

Last edited by umchem; 02-14-13 at 17:07.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On