Getting the last record in SQL in WHERE condition

后端 未结 9 2283
借酒劲吻你
借酒劲吻你 2021-02-15 04:24

i have loanTable that contain two field loan_id and status

loan_id status
==============
1       0
2       9
1       6
         


        
相关标签:
9条回答
  • 2021-02-15 04:29

    Hi if this has not been solved yet. To get the last record for any field from a table the easiest way would be to add an ID to each record say pID. Also say that in your table you would like to hhet the last record for each 'Name', run the simple query

    SELECT Name, MAX(pID) as LastID
    INTO [TableName]
    FROM [YourTableName]
    GROUP BY [Name]/[Any other field you would like your last records to appear by]    
    

    You should now have a table containing the Names in one column and the last available ID for that Name. Now you can use a join to get the other details from your primary table, say this is some price or date then run the following:

    SELECT a.*,b.Price/b.date/b.[Whatever other field you want]
    FROM [TableName] a LEFT JOIN [YourTableName] 
    ON a.Name = b.Name and a.LastID = b.pID
    

    This should then give you the last records for each Name, for the first record run the same queries as above just replace the Max by Min above.

    This should be easy to follow and should run quicker as well

    0 讨论(0)
  • 2021-02-15 04:32

    Since the 'last' row for ID 1 is neither the minimum nor the maximum, you are living in a state of mild confusion. Rows in a table have no order. So, you should be providing another column, possibly the date/time when each row is inserted, to provide the sequencing of the data. Another option could be a separate, automatically incremented column which records the sequence in which the rows are inserted. Then the query can be written.

    If the extra column is called status_id, then you could write:

    SELECT L1.*
      FROM LoanTable AS L1
     WHERE L1.Status_ID = (SELECT MAX(Status_ID)
                             FROM LoanTable AS L2
                            WHERE L2.Loan_ID = 1);
    

    (The table aliases L1 and L2 could be omitted without confusing the DBMS or experienced SQL programmers.)

    As it stands, there is no reliable way of knowing which is the last row, so your query is unanswerable.

    0 讨论(0)
  • 2021-02-15 04:32

    But if last = last inserted, that's not possible for current schema, until a PK addition:

    select top 1 status, loan_id
    from loanTable
    where loan_id = 1
    order by id desc -- PK
    
    0 讨论(0)
  • 2021-02-15 04:32

    In oracle database this is very simple.

    select * from (select * from loanTable order by rownum desc) where rownum=1

    0 讨论(0)
  • 2021-02-15 04:37

    I assume that with "last status" you mean the record that was inserted most recently? AFAIK there is no way to make such a query unless you add timestamp into your table where you store the date and time when the record was added. RDBMS don't keep any internal order of the records.

    0 讨论(0)
  • 2021-02-15 04:44

    Does your table happen to have a primary id or a timestamp? If not then what you want is not really possible.

    If yes then:

        SELECT TOP 1 status
        FROM loanTable
        WHERE loan_id = 1
        ORDER BY primaryId DESC
        -- or
        -- ORDER BY yourTimestamp DESC
    
    0 讨论(0)
提交回复
热议问题