Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the last record in SQL in WHERE condition

i have loanTable that contain two field loan_id and status

loan_id status
==============
1       0
2       9
1       6
5       3
4       5
1       4  <-- How do I select this??
4       6

In this Situation i need to show the last Status of loan_id 1 i.e is status 4. Can please help me in this query.

like image 364
Dinup Kandel Avatar asked Aug 07 '11 10:08

Dinup Kandel


People also ask

How do I find the last record in SQL?

We can use the ORDER BY statement and LIMT clause to extract the last data. The basic idea is to sort the sort the table in descending order and then we will limit the number of rows to 1. In this way, we will get the output as the last row of the table. And then we can select the entry which we want to retrieve.

How can I get the last row inserted in SQL?

There is no way to ask SQL Server which row was inserted last unless you are doing so in the same batch as the insert. For example, if your table has an IDENTITY column, you can use SCOPE_IDENTITY() (never use @@IDENTITY , since that can be unreliable if you have or will ever add triggers to the source table):

What is the last clause in SQL query?

The LAST() function in Structured Query Language shows the last value from the specified column of the table.

How do I get last 5 entries in SQL?

METHOD 1 : Using LIMIT clause in descending orderof specified rows from specifies row. We will retrieve last 5 rows in descending order using LIMIT and ORDER BY clauses and finally make the resultant rows ascending. Since Employee table has IDs, we will perform ORDER BY ID in our query.


2 Answers

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.

like image 103
Jonathan Leffler Avatar answered Oct 19 '22 08:10

Jonathan Leffler


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
like image 30
Skorpioh Avatar answered Oct 19 '22 10:10

Skorpioh