473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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 5692
NeoPa
32,556 Expert Mod 16PB
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 100+
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 Expert Mod 16PB
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
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 Expert Mod 16PB
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 Expert Mod 2GB
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 Expert Mod 16PB
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...

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.