Connecting Tech Pros Worldwide Forums | Help | Site Map

Running totals in a query?

Tony Williams
Guest
 
Posts: n/a
#1: Nov 13 '05
I have a database that hold s figures for each quarter. I want to create a
query whereby the user can ask for the running total of the data upto the
quarter they enter into a prompt. The database stores the quarter name in
txtmonthlabel (a date field) and the quarters totals in txtdomic (a number
field) EG If the user enters March 2004 they get figures upto March 2004, if
they enter June 2004 they get total upto June 2004, in other words the query
would add March's figures to June's figures to give the totals, not just
show June's figures.
Can this be done in a query and what would the prompt criteria be?
TIA
Tony Williams



Allen Browne
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Running totals in a query?


You have a GROUP BY query, so drag the date field into the grid.
In the Total row, under this field, choose Where.
In the Criteria row, enter:[color=blue]
>= [Quarter Starting Date] And < DateAdd("q", 1, [Quarter Starting[/color]
Date])

To ensure the user enters a valid date, choose Paramters from the Query
menu, and enter:
[Quarter Starting Date] Date

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Tony Williams" <tw@tcpinvalid.com> wrote in message
news:cnih26$fd6$1@hercules.btinternet.com...[color=blue]
>I have a database that hold s figures for each quarter. I want to create a
> query whereby the user can ask for the running total of the data upto the
> quarter they enter into a prompt. The database stores the quarter name in
> txtmonthlabel (a date field) and the quarters totals in txtdomic (a number
> field) EG If the user enters March 2004 they get figures upto March 2004,
> if
> they enter June 2004 they get total upto June 2004, in other words the
> query
> would add March's figures to June's figures to give the totals, not just
> show June's figures.
> Can this be done in a query and what would the prompt criteria be?
> TIA
> Tony Williams[/color]


Tony Williams
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Running totals in a query?


Thanks Allen, have followed your guide but when I enter say June 2003 I only
get the figures for the quarterJune 2003, I don't get the figures for
January to June 2003. In other words I should be getting the total of
March's figures and June's Figures
Any help?
TIA
Tony Williams
"Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote in message
news:419cca43$0$25769$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=blue]
> You have a GROUP BY query, so drag the date field into the grid.
> In the Total row, under this field, choose Where.
> In the Criteria row, enter:[color=green]
> >= [Quarter Starting Date] And < DateAdd("q", 1, [Quarter Starting[/color]
> Date])
>
> To ensure the user enters a valid date, choose Paramters from the Query
> menu, and enter:
> [Quarter Starting Date] Date
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Tony Williams" <tw@tcpinvalid.com> wrote in message
> news:cnih26$fd6$1@hercules.btinternet.com...[color=green]
> >I have a database that hold s figures for each quarter. I want to create[/color][/color]
a[color=blue][color=green]
> > query whereby the user can ask for the running total of the data upto[/color][/color]
the[color=blue][color=green]
> > quarter they enter into a prompt. The database stores the quarter name[/color][/color]
in[color=blue][color=green]
> > txtmonthlabel (a date field) and the quarters totals in txtdomic (a[/color][/color]
number[color=blue][color=green]
> > field) EG If the user enters March 2004 they get figures upto March[/color][/color]
2004,[color=blue][color=green]
> > if
> > they enter June 2004 they get total upto June 2004, in other words the
> > query
> > would add March's figures to June's figures to give the totals, not just
> > show June's figures.
> > Can this be done in a query and what would the prompt criteria be?
> > TIA
> > Tony Williams[/color]
>
>[/color]


John Winterbottom
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Running totals in a query?


"Tony Williams" <tw@tcpinvalid.com> wrote in message
news:cnih26$fd6$1@hercules.btinternet.com...[color=blue]
>I have a database that hold s figures for each quarter. I want to create a
> query whereby the user can ask for the running total of the data upto the
> quarter they enter into a prompt. The database stores the quarter name in
> txtmonthlabel (a date field) and the quarters totals in txtdomic (a number
> field) EG If the user enters March 2004 they get figures upto March 2004,
> if
> they enter June 2004 they get total upto June 2004, in other words the
> query
> would add March's figures to June's figures to give the totals, not just
> show June's figures.
> Can this be done in a query and what would the prompt criteria be?[/color]


You can do this easily, (for example with a subquery). Post your table
structure with some sample data and the output you need and someone will be
able to help.


Tony Williams
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Running totals in a query?


Thanks John what's a subquery tho?
Tony
"John Winterbottom" <assaynet@hotmail.com> wrote in message
news:3045mfF2rt9u4U1@uni-berlin.de...[color=blue]
> "Tony Williams" <tw@tcpinvalid.com> wrote in message
> news:cnih26$fd6$1@hercules.btinternet.com...[color=green]
> >I have a database that hold s figures for each quarter. I want to create[/color][/color]
a[color=blue][color=green]
> > query whereby the user can ask for the running total of the data upto[/color][/color]
the[color=blue][color=green]
> > quarter they enter into a prompt. The database stores the quarter name[/color][/color]
in[color=blue][color=green]
> > txtmonthlabel (a date field) and the quarters totals in txtdomic (a[/color][/color]
number[color=blue][color=green]
> > field) EG If the user enters March 2004 they get figures upto March[/color][/color]
2004,[color=blue][color=green]
> > if
> > they enter June 2004 they get total upto June 2004, in other words the
> > query
> > would add March's figures to June's figures to give the totals, not just
> > show June's figures.
> > Can this be done in a query and what would the prompt criteria be?[/color]
>
>
> You can do this easily, (for example with a subquery). Post your table
> structure with some sample data and the output you need and someone will[/color]
be[color=blue]
> able to help.
>
>[/color]


Allen Browne
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Running totals in a query?


So you want all the values so far in this calendar year?
Ask the user for the ending date:

Between DateSerial(Year([EndDate]),1,1) And [EndDate]

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Tony Williams" <tw@tcpinvalid.com> wrote in message
news:cnileg$p7m$1@titan.btinternet.com...[color=blue]
> Thanks Allen, have followed your guide but when I enter say June 2003 I
> only
> get the figures for the quarterJune 2003, I don't get the figures for
> January to June 2003. In other words I should be getting the total of
> March's figures and June's Figures
> Any help?
> TIA
> Tony Williams
> "Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote in message
> news:419cca43$0$25769$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=green]
>> You have a GROUP BY query, so drag the date field into the grid.
>> In the Total row, under this field, choose Where.
>> In the Criteria row, enter:[color=darkred]
>> >= [Quarter Starting Date] And < DateAdd("q", 1, [Quarter Starting[/color]
>> Date])
>>
>> To ensure the user enters a valid date, choose Paramters from the Query
>> menu, and enter:
>> [Quarter Starting Date] Date
>>
>>
>> "Tony Williams" <tw@tcpinvalid.com> wrote in message
>> news:cnih26$fd6$1@hercules.btinternet.com...[color=darkred]
>> >I have a database that hold s figures for each quarter. I want to create[/color][/color]
> a[color=green][color=darkred]
>> > query whereby the user can ask for the running total of the data upto[/color][/color]
> the[color=green][color=darkred]
>> > quarter they enter into a prompt. The database stores the quarter name[/color][/color]
> in[color=green][color=darkred]
>> > txtmonthlabel (a date field) and the quarters totals in txtdomic (a[/color][/color]
> number[color=green][color=darkred]
>> > field) EG If the user enters March 2004 they get figures upto March[/color][/color]
> 2004,[color=green][color=darkred]
>> > if
>> > they enter June 2004 they get total upto June 2004, in other words the
>> > query
>> > would add March's figures to June's figures to give the totals, not
>> > just
>> > show June's figures.
>> > Can this be done in a query and what would the prompt criteria be?
>> > TIA
>> > Tony Williams[/color][/color][/color]


Tony Williams
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Running totals in a query?


Thanks Allen worked like a dream. If you get chance though could you explain
what's happening in that statement? Although it works I would like to try
and understand why it works.
Thanks
Tony
"Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote in message
news:419d4878$0$25770$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=blue]
> So you want all the values so far in this calendar year?
> Ask the user for the ending date:
>
> Between DateSerial(Year([EndDate]),1,1) And [EndDate]
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Tony Williams" <tw@tcpinvalid.com> wrote in message
> news:cnileg$p7m$1@titan.btinternet.com...[color=green]
> > Thanks Allen, have followed your guide but when I enter say June 2003 I
> > only
> > get the figures for the quarterJune 2003, I don't get the figures for
> > January to June 2003. In other words I should be getting the total of
> > March's figures and June's Figures
> > Any help?
> > TIA
> > Tony Williams
> > "Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote in message
> > news:419cca43$0$25769$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=darkred]
> >> You have a GROUP BY query, so drag the date field into the grid.
> >> In the Total row, under this field, choose Where.
> >> In the Criteria row, enter:
> >> >= [Quarter Starting Date] And < DateAdd("q", 1, [Quarter Starting
> >> Date])
> >>
> >> To ensure the user enters a valid date, choose Paramters from the Query
> >> menu, and enter:
> >> [Quarter Starting Date] Date
> >>
> >>
> >> "Tony Williams" <tw@tcpinvalid.com> wrote in message
> >> news:cnih26$fd6$1@hercules.btinternet.com...
> >> >I have a database that hold s figures for each quarter. I want to[/color][/color][/color]
create[color=blue][color=green]
> > a[color=darkred]
> >> > query whereby the user can ask for the running total of the data upto[/color]
> > the[color=darkred]
> >> > quarter they enter into a prompt. The database stores the quarter[/color][/color][/color]
name[color=blue][color=green]
> > in[color=darkred]
> >> > txtmonthlabel (a date field) and the quarters totals in txtdomic (a[/color]
> > number[color=darkred]
> >> > field) EG If the user enters March 2004 they get figures upto March[/color]
> > 2004,[color=darkred]
> >> > if
> >> > they enter June 2004 they get total upto June 2004, in other words[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> >> > query
> >> > would add March's figures to June's figures to give the totals, not
> >> > just
> >> > show June's figures.
> >> > Can this be done in a query and what would the prompt criteria be?
> >> > TIA
> >> > Tony Williams[/color][/color]
>
>[/color]


John Winterbottom
Guest
 
Posts: n/a
#8: Nov 13 '05

re: Running totals in a query?


"Tony Williams" <tw@tcpinvalid.com> wrote in message
news:cnissa$eo$1@hercules.btinternet.com...[color=blue]
> Thanks John what's a subquery tho?[/color]

From Access Help:

"SQL Subqueries
A subquery is a SELECT statement nested inside a SELECT, SELECT...INTO,
INSERT...INTO, DELETE, or UPDATE statement or inside another subquery"

This example shows cumulative sales in the Northwind database:

----------------------------------------
select o.orderID, o.orderDate,
(
select sum(d2.unitPrice*d2.quantity*(100-d2.discount)/100)
from [Order Details] as d2
inner join orders as o2 on d2.orderID = o2.orderID
where o2.orderDate <= o.orderDate
)
from orders as o
----------------------------------------

This is a special case of subquery called a correlated subquery - the line

"where o2.orderDate <= o.orderDate"

restricts rows in the inner query based on the value of a column in the
outer query. A correlated subquery can be used to find running totals,
sliding aggregates etc.





Allen Browne
Guest
 
Posts: n/a
#9: Nov 13 '05

re: Running totals in a query?


Since EndDate doesn't match anything else, Access assumes it's a parameter
and pops up a dialog for you to enter a value.

The Year() function extracts the year from a date.
The DateSerial() function builds a date from a year, month, and day.

The criteria therefore reads the year of the date you entered, and builds a
date for the 1st day of the year. The WHERE clause therefore asks for all
dates between the first of the year and the date you entered.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Tony Williams" <tw@tcpinvalid.com> wrote in message
news:cnkses$ifs$1@titan.btinternet.com...[color=blue]
> Thanks Allen worked like a dream. If you get chance though could you
> explain
> what's happening in that statement? Although it works I would like to try
> and understand why it works.
> Thanks
> Tony
> "Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote in message
> news:419d4878$0$25770$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=green]
>> So you want all the values so far in this calendar year?
>> Ask the user for the ending date:
>>
>> Between DateSerial(Year([EndDate]),1,1) And [EndDate]
>>
>>
>> "Tony Williams" <tw@tcpinvalid.com> wrote in message
>> news:cnileg$p7m$1@titan.btinternet.com...[color=darkred]
>> > Thanks Allen, have followed your guide but when I enter say June 2003 I
>> > only
>> > get the figures for the quarterJune 2003, I don't get the figures for
>> > January to June 2003. In other words I should be getting the total of
>> > March's figures and June's Figures
>> > Any help?
>> > TIA
>> > Tony Williams
>> > "Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote in message
>> > news:419cca43$0$25769$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
>> >> You have a GROUP BY query, so drag the date field into the grid.
>> >> In the Total row, under this field, choose Where.
>> >> In the Criteria row, enter:
>> >> >= [Quarter Starting Date] And < DateAdd("q", 1, [Quarter Starting
>> >> Date])
>> >>
>> >> To ensure the user enters a valid date, choose Paramters from the
>> >> Query
>> >> menu, and enter:
>> >> [Quarter Starting Date] Date
>> >>
>> >>
>> >> "Tony Williams" <tw@tcpinvalid.com> wrote in message
>> >> news:cnih26$fd6$1@hercules.btinternet.com...
>> >> >I have a database that hold s figures for each quarter. I want to[/color][/color]
> create[color=green][color=darkred]
>> > a
>> >> > query whereby the user can ask for the running total of the data
>> >> > upto
>> > the
>> >> > quarter they enter into a prompt. The database stores the quarter[/color][/color]
> name[color=green][color=darkred]
>> > in
>> >> > txtmonthlabel (a date field) and the quarters totals in txtdomic (a
>> > number
>> >> > field) EG If the user enters March 2004 they get figures upto March
>> > 2004,
>> >> > if
>> >> > they enter June 2004 they get total upto June 2004, in other words[/color][/color]
> the[color=green][color=darkred]
>> >> > query
>> >> > would add March's figures to June's figures to give the totals, not
>> >> > just
>> >> > show June's figures.
>> >> > Can this be done in a query and what would the prompt criteria be?
>> >> > TIA
>> >> > Tony Williams[/color][/color][/color]


Closed Thread