简体   繁体   中英

Get the total number of previous records

I am creating a query in MS Access that needs to update newly created field with the total number of previous records. Something like this:

Original table:

|  ID | Name |
--------------
|  1  | Mark |
--------------
|  2  | Mich |
--------------
|  3  | Isak |
--------------

Now I add another column ( of type int ), eg Ordinal_Number :

|  ID | Name | Ordinal_Number |
-------------------------------
|  1  | Mark |                |
-------------------------------
|  2  | Mich |                | 
-------------------------------
|  3  | Isak |                | 
-------------------------------

Now I need to fill Ordinal_Number with the number of previous records :

|  ID | Name | Ordinal_Number |
-------------------------------
|  1  | Mark |       0        |
-------------------------------
|  2  | Mich |       1        | 
-------------------------------
|  3  | Isac |       2        | 
-------------------------------

I couldn't even get the idea how to try fighting this problem so I am asking here.

 SELECT a.*, (SELECT COUNT(*) FROM Original_table WHERE a.ID>=ID) as Ordinal_Number
 FROM Original_table as a
 ORDER BY a.ID

For the update:

UPDATE Original_table o
set  Ordinal_Number=
(SELECT COUNT(*) FROM Original_table WHERE o.ID>=ID)

or for jet4 version

UPDATE Original_table o 
inner join
(SELECT a.*, (SELECT COUNT(*) FROM Original_table WHERE a.ID>=ID) as Ordinal_Number
 FROM Original_table as a
 ORDER BY a.ID) newt
on o.ID=newt.ID
Set o.Ordinal_Number=newt.Ordinal_Number

In order to even begin to talk about getting values from a previous record, you have to define what "previous" means in the context of your table. For most purposes, Access treats a table as a "big bag of records", with no defined order -- most specifically, records are not inherently ordered by entry sequence. In any situation where you want to talk about "previous" or "next" records, you must specify a sort order for the records -- for example, in the ORDER BY clause of a query. And that sort order must be defined in terms of data in the records.

If you need to process records in order of entry, then you need to have a date/time field in the records that is set to Now() whenever a record is entered. If your records have a consecutive autonumber primary key, then In carefully restricted circumstances, you might use that key field as an indicator of the entry sequence. However, not all tables use autonumber keys, and even in those that do, autonumbers can become random, not consecutive, under certain circumstances. So if you care about entry sequence, you really ought to use a date/time field to store the information.

Once you have established a field that you can use to determine the sequence of records, you don't really need to store the current inventory total in any record. It can always be calculated on the fly by adding up the transaction quantities of all records entered up through the current one. In some cases, it may be useful to store it for efficiency's sake, but that runs the risk of having inconsistent data if, for example, somebody modifies one of the earlier records without recalculating the total in all subsequent records. So I don't recommend storing this calculated data unless you find you really need to.

The ordinal number starts with 0 , so I would do:

update Original
    set Ordinal_Number = (select count(*)
                          from Original as o2 
                          where o2.ID <= original.ID
                         ) - 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM