Results 1 to 4 of 4
  1. #1
    =Roman is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jun 2016
    Posts
    2

    Question iif referring to previous record

    Hi

    I have a simple table a series of times (in # mins from midnight Jan 1st), and categories

    time cat
    1 a
    10 r
    12 f
    25 h
    100 reset
    123 h
    140 s

    And I want to put a 'block number' that adds one each time it gets to 'reset', i.e. giving

    1 a 1
    10 r 1
    12 f 1
    25 h 1
    100 reset 2
    123 h 2
    140 s 2



    in excel this would be quite simple, with an if(cell 1 up="Reset",cell above+1,cell above), but I have 31m records, so don't want to have to export and reimport this bit by bit if possible

    However, I've not got much of an idea in how to do this, or even if it can be done, using an iif in access?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    assuming your 'time' field is a long (and by the way, time is a reserved word, so change it to something else), you don't need to store the block number, just calculate when required. Also ensure both time and cat fields are indexed, otherwise the process will be unacceptably slow.

    I'm also assuming that time starts from 0 and continues to increment. In a query put the following in a new column

    BlockNo: (SELECT count(*) FROM myTable AS T WHERE cat='reset' AND time<=myTable.time)+1

    change names to suit

    The excel option would be simple if a) you could list 31m records in a worksheet (limit is 1m) and b) it is sorted in the right order in the first place
    Last edited by CJ_London; 06-23-2016 at 03:02 AM. Reason: missed the +1

  3. #3
    =Roman is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jun 2016
    Posts
    2
    Thanks for the reply - that sounds like it makes sense, but I can't seem to get it to work (syntax error).

    Here's a an extract of the table with the actual names for reference - and it's OFF for Reset. I don't think it needs the time element to be referenced, as it's just counting on the Network_From field - everything is sorted in the correct order, (i.e. by household and person, then by time).


    BlockNo: (SELECT count(*) FROM [Tbl_Final_Switching] AS T WHERE [Tbl_Final_Switching]![Network_From] ='OFF')+1

    Click image for larger version. 

Name:	example.png 
Views:	8 
Size:	8.7 KB 
ID:	24999

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    Access is not a 'big excel' it is a completely different beast

    1. you havent used the same syntax as I provided - you use . not !
    2. you are missing some of the criteria
    I don't think it needs the time element to be referenced, as it's just counting on the Network_From field - everything is sorted in the correct order, (i.e. by household and person, then by time).
    no it's not - interpret the code, it is counting where the switch time is less than the record switchtime
    3. you are incorrectly referring to the main table not the aliased table. Try

    BlockNo: (SELECT count(*) FROM Tbl_Final_Switching AS T WHERE Network_From ='OFF' AND Switch_Time<=Tbl_Final_Switching.Switch_Time)+1

    the sql to your query should look something like

    SELECT *, (SELECT count(*) FROM Tbl_Final_Switching AS T WHERE Network_From ='OFF' AND Switch_Time<=Tbl_Final_Switching.Switch_Time)+1 AS BlockNo
    FROM Tbl_Final_Switching
    ORDER BY Switch_Time
    Note for the purposes of calculating the blockno, you do not need to sort/order the table except for checking purposes that you are getting the correct result or for reporting purposes if required.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 04-11-2015, 10:31 AM
  2. Replies: 8
    Last Post: 07-06-2013, 05:13 PM
  3. Replies: 3
    Last Post: 08-22-2012, 09:23 AM
  4. Replies: 22
    Last Post: 06-12-2012, 10:02 PM
  5. Replies: 2
    Last Post: 11-26-2010, 04:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums