473,508 Members | 3,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating values defined by their #

Dear all,
I have a simple table, with only the following fields:
- Key
- MyDate
- Price
I would like to biuld a Query which uses these fields and which make some
other calculations on the Price.

To do one calculation, I need to get the difference between the last two
"Price", to get a "ModifiedPrice".

This difference would be this one:
Difference = "LastPrice" - "PenultimatePrice"

Then my ModifiedPrice would be simply equal to Price + Difference...

1. Could you help me finding the good way to do this rather simple
calculation, either directly in my query, or through a function?

2. I also would like to be able to calculate the percentage difference of the
prices.
The calculation would be:
Return : (Price / Price[-1] ) -1

However, I do not know how to find the number "Price[-1]" in Access.
Thanks a lot for your help.

Regards,

Laurent

Sep 9 '06 #1
3 1738
laurentc wrote:
Dear all,
I have a simple table, with only the following fields:
- Key
- MyDate
- Price
I would like to biuld a Query which uses these fields and which make some
other calculations on the Price.

To do one calculation, I need to get the difference between the last two
"Price", to get a "ModifiedPrice".

This difference would be this one:
Difference = "LastPrice" - "PenultimatePrice"

Then my ModifiedPrice would be simply equal to Price + Difference...

1. Could you help me finding the good way to do this rather simple
calculation, either directly in my query, or through a function?

2. I also would like to be able to calculate the percentage difference of the
prices.
The calculation would be:
Return : (Price / Price[-1] ) -1

However, I do not know how to find the number "Price[-1]" in Access.
Thanks a lot for your help.

Regards,

Laurent
This might do the trick. I used the following sample data:

KEY MYDATE PRICE
1 1/1/2006 $10.00
2 2/1/2006 $15.00
3 3/1/2006 $14.00 (*)
4 4/1/2006 $23.00 (*)
5 2/5/2006 $18.00

(*) = the last two prices

SELECT (FIRST(THEPRICE)-LAST(THEPRICE)) AS DIFFERENCE
FROM (SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC);

Returns: $9 (= $23 - $14)

SELECT (FIRST(THEPRICE)/LAST(THEPRICE)-1) AS PERCENTAGE
FROM
(SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC);

Returns: 0.64285714286 (= 23/14 - 1)

Side note: It's probably something wrong with my DB but when I save
these queries and open them up again, they turn into something like this:

SELECT (FIRST(THEPRICE)-LAST(THEPRICE)) AS DIFFERENCE
FROM [SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC].
AS [%$##@_Alias];

Weird.

HTH
--
Smartin
Sep 9 '06 #2
Smartin wrote:
Side note: It's probably something wrong with my DB but when I save
these queries and open them up again, they turn into something like this:

SELECT (FIRST(THEPRICE)-LAST(THEPRICE)) AS DIFFERENCE
FROM [SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC].
AS [%$##@_Alias];
Following up on my last comment, and OT to the thread, it seems Access
wants to create an alias for the subquery.

The following works:
SELECT (FIRST(THEPRICE)-LAST(THEPRICE)) AS DIFFERENCE
FROM (SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC)
AS SUBQ;

Though Access changes it to:
SELECT (FIRST(THEPRICE)-LAST(THEPRICE)) AS DIFFERENCE
FROM [SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC].
AS SUBQ;

Still a little odd, methinks. I don't recall seeing Access change
parentheses to square brackets around a subquery before. Also note
Access inserts a dot after the subquery and before the alias.

--
Smartin
Sep 10 '06 #3
Smartin, that SUBQUERY works fine.

Thanks a lot for your help.
Regards,

Laurent

Smartin wrote:
>Side note: It's probably something wrong with my DB but when I save
these queries and open them up again, they turn into something like this:

SELECT (FIRST(THEPRICE)-LAST(THEPRICE)) AS DIFFERENCE
FROM [SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC].
AS [%$##@_Alias];

Following up on my last comment, and OT to the thread, it seems Access
wants to create an alias for the subquery.

The following works:
SELECT (FIRST(THEPRICE)-LAST(THEPRICE)) AS DIFFERENCE
FROM (SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC)
AS SUBQ;

Though Access changes it to:
SELECT (FIRST(THEPRICE)-LAST(THEPRICE)) AS DIFFERENCE
FROM [SELECT TOP 2 PRICE AS THEPRICE FROM PRICES ORDER BY MYDATE DESC].
AS SUBQ;

Still a little odd, methinks. I don't recall seeing Access change
parentheses to square brackets around a subquery before. Also note
Access inserts a dot after the subquery and before the alias.
--
Message posted via http://www.accessmonster.com

Sep 11 '06 #4

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

Similar topics

0
2255
by: Kasp | last post by:
Hi there, I am trying to make an OLAP cube on a table having two columns (datetime, Number_of_times_an_event_occured). My dimension is time and I want to measure the Min and Max times an event...
10
4239
by: Howard Martin | last post by:
I have a form at http://www.develop.check.com.au/hh/order.html where an extended sub-total and then a grand total is calculated as soon as a quantity is input by a user. I would like to be able to...
12
23661
by: Anthony Robinson | last post by:
Is anyone aware of a function (system or user defined) that will calculate business days? For instance: I have a column in as table called DATE. I want to be able to add five business days to that...
4
2130
by: Neil Grantham | last post by:
I am creating a database for Nursery children, and part of the reporting requirement, is to show those on the waiting list. Children will be elligible for entry by age, and so based on their...
7
2126
by: JLM | last post by:
I have a table that has fieldA, fieldB, fieldC. I want fieldC=fieldA-fieldB. simple enough. the next record I want to be able to do the same on the new value of fieldC. I can do this with SAP...
1
2503
by: Megan | last post by:
quick summary: i'm having problems trying to group fields in a report in order to calculate percentages. to calculate percentages, i'm comparing the results from my grouped fields to the totals....
22
2105
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
1
1564
by: Nixeh | last post by:
Hey guys, New to the forums but ive hit a snag whilst updating a Access Database at work. Currently trying to repair and maintain a database that has been built by 6 people all with even worse...
8
2310
by: sajid | last post by:
The CSS 2.1 Specification describes how to sort a list of selectors in order of specificity, but it doesn't provide a method to calculate the specificity of a single selector in isolation. I've...
43
17165
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
0
7228
marktang
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,...
0
7128
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...
0
7393
jinu1996
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...
0
7502
tracyyun
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...
0
5635
agi2029
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,...
1
5057
isladogs
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...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.