473,480 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to get next row value in query rows (follow-up question)

2 New Member
I’m trying to implement the recommended solution regarding “How to get next row…” that was posted July 12, 2008, but am having trouble with the “Autonum” part using Access 2003. In this case I am trying to put two rows of price data side by side so that I can calculate the daily percent change.
------------------------------------------ Solution------------------------------------------
"when you add an autonum field to the table you can create a query with:
Select autonumfield-1 as Oldkey... "
[ and later join this with the original table to get data side-by-side]
Expand|Select|Wrap|Line Numbers
  1.  ------------------------------------------ Table  ------------------------------------------
  2. Price_date    Price    Dividend    RecNum
  3. 8/1/2008    $10.00    $1.00    {916E47CB-015A-466D-A2D0-...}
  4. 8/2/2008    $11.00    $1.50    {CDE5006B-5D05-4A35-B144-...}
  5. 8/3/2008    $12.00    $2.00    {AF81D70E-B18F-405F-9D87-...}
Expand|Select|Wrap|Line Numbers
  1. -------------------------------------------- SQL  -------------------------------------------
  2. SELECT RecNum-1 AS RecNum2, Auto_Price_tbl.Price_date, Auto_Price_tbl.Price, Auto_Price_tbl.Dividend
  3. FROM Auto_Price_tbl
  4. WITH OWNERACCESS OPTION;
Expand|Select|Wrap|Line Numbers
  1. ------------------------------------Query Output  ------------------------------------------
  2. RecNum2    Price_date    Price    Dividend
  3. #Error    8/1/2008    $10.00    $1.00
  4. #Error    8/2/2008    $11.00    $1.50
  5. #Error    8/3/2008    $12.00    $2.00
I have tried the query using variation of the Select statement:
“RecNum-1”, “RecNum – 1”, “[RecNum-1]” and “[RecNum]-1”
all to no result. There must be something about the solution that I don’t fully understand yet. Can you suggest where I might be going wrong?
Aug 5 '08 #1
7 5695
NeoPa
32,556 Recognized Expert Moderator MVP
I've searched for the thread you mention but was unable to find it.

Perhaps you could post a link - then we could start to look into this for you.
Aug 7 '08 #2
hjozinovic
167 New Member
I believe this is the old post:
Link

Also, I managed to get what I believe is the solution.
I noticed you're using RecNum as join field and trying to calculate RecNum-1. It looks to me RecNum is textual field and therefore can not be used in calculations.
What I did is this:
1. I created a table containing all the fields you listed and added a new field named ID (autonumber)
2. I created a Query1 based on that Table1 including all the fields and adding a new field: OldID:[ID]-1
3. I created second query based on table and query1.
These two are left joined Table1.ID->Query1.OldID
this query2 contains all the fields from Table and PriceDate field from query2

Here is how it looks:

[HTML]ID Table1.PriceDate Price Dividend RecNum Query1.PriceDate
1 01.08.2008 10,00 1,00 {916E47CB-015A-466D-A2D0-...} 02.08.2008
2 02.08.2008 11,00 2,00 {CDE5006B-5D05-4A35-B144-...} 03.08.2008
3 03.08.2008 12,00 2,00 {AF81D70E-B18F-405F-9D87-...}[/HTML]
hope this is what you wanted to have.
Aug 8 '08 #3
NeoPa
32,556 Recognized Expert Moderator MVP
I believe this is the old post:
Link
...
Good for you H.

I'll wait to see how the OP fares with this :)
Aug 8 '08 #4
FinanceGuy2
2 New Member
Thanks guys! This did the trick. Although I was using "autonum" for the field, I had it set to field format of "replication ID" rather than "integer long". I thinks some MS help page suggested this and I foolishly followed that lead!
Aug 22 '08 #5
NeoPa
32,556 Recognized Expert Moderator MVP
Foolish FinanceGuy!

That's like copying their example code. It never follows their own naming conventions even :D It is rarely any good for anything other than illustrating the point and giving an example in context. It does usually help - but is always worth taking with a pinch of salt.

PS. With my fairer hat on, this isn't entirely true in all cases. Some follow that trend, but I have found some reasonable code on MSDN and other MS provided resources.
Aug 23 '08 #6
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi all. I'd be a little cautious about using [ID]-1 as a join to return the previous row along with the next, unless you are certain that the autonumbers are entirely contiguous throughout. If the autonumber has skipped one or more values the [ID]-1 join will fail to return certain rows. This is mentioned by Nico in his final post in the linked thread.

As you know, autonumbers are never re-used, so if a user simply cancels entry of the current record (or makes an entry then subsequently deletes the record) the gap between the autonumber for the previous record and the next in sequence will be at least 2, not 1.

I use the max of the joined ID where less than the current ID for such joins, as it works regardless of the gap in value between consecutive autonumbers.

Another point to consider is that it is implicit in this case that the rows will always be entered in ascending date order. This may well always be true for this application, but in other circumstances larger autonumber IDs might not correspond to later date entries - a user may enter data in descending date order, or not in order at all, in which case matching the previous ID does not correspond to matching the previous date.

-Stewart
Aug 23 '08 #7
NeoPa
32,556 Recognized Expert Moderator MVP
Following on from Stewart's post, linking on the Max(subQ.PriceDate), WHERE subQ.PriceDate < Auto_Price_Tbl.PriceDate might be the safest and most appropriate way to link to the previous record in this case.
Aug 24 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2838
by: Stewart Allen | last post by:
I'm trying to find the next value in a table that a student needs to achieve once he has already passed one grade. *tblStudents* StudentID (PK) FirstName *tblGradingDates* DateID ...
4
1034
by: Earl | last post by:
Is it poor practice to have one custom library depend on another custom library? For example, my data manipulation library depends on my replication library, which in turn pulls serial data out of...
4
59692
by: Diego | last post by:
Hi everybody! I wonder if there is an sql command that increases a sequence value by one and returns such a value. I would like to manage an id inside a program that connects to the database...
5
3496
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
1
2319
by: aaron1234nz | last post by:
I am trying to write a stored procedure add a piece of data from table2 into another table1. I thought the query was going to be easy, but I was misteken. All I am trying to achieve is: for...
2
2018
by: de_ja | last post by:
Hi, If, for example an access database that concerns insurance claim investigations. The database comprises only one table containing data - investigation reference number, referral date,...
8
1587
by: Shila | last post by:
i am doing online meter project......there is table in my html page...in that table meter show continuesly diff values............. without resfrshing page table can take next value..... that is my...
4
7882
by: qwedster | last post by:
Folks, How to the next value of identity(1,1) before insert? SELECT IDENT_CURRENT('ExampleTable') + IDENT_INCR('ExampleTable'); --does not work when the table is empty! USE GO
2
3179
by: digituf | last post by:
i have 2 forms here. 1) DisplayDetails.php 2) RegistrationForm.php when user click to the link 'Next' at the DisplayDetails.php page it will bring all the session value to the...
0
7039
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
7037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2992
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
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.