Finding the most recent record in a table with otherwiseidentical field values

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Finding the most recent record in a table with otherwiseidentical field values

    Hello,

    Would anyone be able to share how they find the MOST recent record among otherwise identical records in for example a transaction table?
    kbergstrom Guest

  2. Similar Questions and Discussions

    1. How can i retrieve record ONLY From 300 - 400 in amillion Record Table?
      HI all Thanks for your time.. I have a question here.. How can i retrieve record ONLY From XXX - XXX in a million Record Table? eg. I have a...
    2. Finding the Record-no after CFINSERT
      I've created a vehicle maintence cf database. On the new record page the user enters the service details and then picks parts from a like that have...
    3. Compare record field with another record field of samerecordset
      :confused; ... but I don't know how to refer to field of next record in loop through recordset. Any ideas? Thanks, WolfShade
    4. finding first unread record
      I am looking for a simple way to query a table and pull up the first record with an empty value in the date_processed column. I have tried a...
    5. Finding null values in a large table
      I have a fairly large table (millions of rows) and I would like to be able to find rows that have a null value in a particular column. I know that...
  3. #2

    Default Re: Finding the most recent record in a table withotherwise identical field values

    If you have an autonumber id in your table, do a SELECT MAX(id) AS maxId FROM
    myTable. Then use maxId in the WHERE clause of your query to get the most
    recent record. Of course, you can combine this into one query.

    jdeline Guest

  4. #3

    Default Re: Finding the most recent record in a table withotherwise identical field values

    If you don't have a lastupdatedat date/time field, and you can add one, do so.

    Or, assuming you have a single field primary key
    select max(primarykeyfield)
    from thetable
    where etc.

    make your where clause as long as you can.

    Dan Bracuk Guest

  5. #4

    Default Re: Finding the most recent record in a table withotherwise identical field values

    Assuming you have no indexes on your table
    if you do a select * from tableA then the first record should be the first
    record inserted etc
    Remember no indexes and you can not sort the table

    [url]http://sqlservercode.blogspot.com/[/url]


    SQLMenace Guest

  6. #5

    Default Re: Finding the most recent record in a table withotherwise identical field values

    I must be doing something wrong. In a series of transactions for the same item
    within a given table, I want to see the entire record for that item that is
    most recent. I also want to see the most recent record for all other items in
    that same table as well which will have a different transaction ID. The Max
    function seems to return only a single value when selecting on transaction_ID.
    Any other ideas?

    kbergstrom Guest

  7. #6

    Default Re: Finding the most recent record in a table withotherwise identical field values

    I'm not sure what your table structure is, but if your data looks something
    like this ....

    RecordID | ItemID | Other Fields
    ----------------------------------
    1 | 31 | stuff...
    ----------------------------------
    2 | 31 | stuff...
    ----------------------------------
    3 | 31 | stuff...
    ----------------------------------

    And you want to pull information for the most recent record for "itemID = 31",
    you would need one query to get the max record for each itemID:

    SELECT ItemID, MAX(RecordID) AS MaxRecordID
    FROM yourTable
    GROUP BY ItemID

    ... and then join those results back to the same table to get the details.
    This could be done in a single query. If you need to perform this operation
    frequently, I would suggest creating a "view" in your database that returned
    the MaxRecordID for each item.






    mxstu Guest

  8. #7

    Default Re: Finding the most recent record in a table withotherwise identical field values

    Yes, that works for a single item ID. I was looking for something that would
    return the latest records for all item ID's. I have almost 20,000 items in that
    table, with over 76,000 transactions. What I want returned is the 20,000 most
    recent transactions for those items.

    I am not familiar with creating the view that you mentioned for all 20,000
    items. Is there some guidance online that you can point me to?

    kbergstrom Guest

  9. #8

    Default Re: Finding the most recent record in a table withotherwise identical field values

    Yes. The sql statement I posted would give you the most recent transactions
    for all items in your table (assuming there are no syntax errors). Although
    you might need to use a LEFT JOIN if you want to include records that have (0)
    transactions.

    A "view" is just a virtual table. It allows you to present information in a
    different way without creating a separate table.
    [url]http://www.w3schools.com/sql/sql_view.asp[/url]

    If you plan to do this frequently, I would suggest creating a "view" that
    returned the max transaction for all items. You could then join to the "view"
    in your query. So instead of using this ...

    SELECT i.RecordID, i.ItemID, i.OtherFields
    FROM yourTable i INNER JOIN
    ( SELECT ItemID, MAX(RecordID) AS MaxRecordID
    FROM yourTable
    GROUP BY ItemID
    ) as maxI
    ON i.RecordID = maxI.RecordID

    You would join to the view ....

    SELECT i.RecordID, i.ItemID, i.OtherFields
    FROM yourTable i INNER JOIN getMaxTransactionIDView
    ON i.RecordID = maxI.RecordID


    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139