locked
How to fetch last match data.. RRS feed

  • Question

  • hi..

    i have one table conatins ID,carno,houseno,entrytime and exit time.....

    i need to fetch last insert data which match houseno,carno and id....in every time insertion happen thn entry and exit time is differnet so i want to fetch based on matching criteria of houseno,carno and id....after fetching that last entry i want to check data in entry and exit column if anyone contain null value means no data......

    Wednesday, April 2, 2014 10:28 AM

Answers

  • i want to fetch  last record from table which match houseno,carno.......

    after fetching last match row from table i want to check that last row have data in entry and exit field

    explain what according to you represent "last". There's no concept of first,last etc in sql table unless you specify it in terms of column(s) using ORDER BY. Thats why i asked whether Id is identity field

    in which case you can use

    SELECT TOP 1 houseno,carno,id
    FROM table 
    WHERE houseno = <your house value>
    AND carno = <your carno value>
    ORDER BY Id DESC


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    • Proposed as answer by C0mmander Wednesday, April 2, 2014 12:07 PM
    • Marked as answer by Nitin gupts Wednesday, April 2, 2014 12:24 PM
    Wednesday, April 2, 2014 12:01 PM
  • The above is the correct answer. However if you don't agree to this maybe we can help you if you give us the table schema and some sample entries.

    Is the understanding correct that you are looking for T-SQL code?

    • Edited by C0mmander Wednesday, April 2, 2014 12:08 PM
    • Marked as answer by Nitin gupts Wednesday, April 2, 2014 12:24 PM
    Wednesday, April 2, 2014 12:07 PM

All replies

  • Is ID an identity field? if yes you can use logic like below

    SELECT houseno,carno,id
    FROM table 
    ORDER BY Id DESC

    to get last inserted value


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Wednesday, April 2, 2014 10:52 AM
  • i want to fetch  last record from table which match houseno,carno.......

    after fetching last match row from table i want to check that last row have data in entry and exit field

    Wednesday, April 2, 2014 11:14 AM
  • i want to fetch  last record from table which match houseno,carno.......

    after fetching last match row from table i want to check that last row have data in entry and exit field

    explain what according to you represent "last". There's no concept of first,last etc in sql table unless you specify it in terms of column(s) using ORDER BY. Thats why i asked whether Id is identity field

    in which case you can use

    SELECT TOP 1 houseno,carno,id
    FROM table 
    WHERE houseno = <your house value>
    AND carno = <your carno value>
    ORDER BY Id DESC


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    • Proposed as answer by C0mmander Wednesday, April 2, 2014 12:07 PM
    • Marked as answer by Nitin gupts Wednesday, April 2, 2014 12:24 PM
    Wednesday, April 2, 2014 12:01 PM
  • The above is the correct answer. However if you don't agree to this maybe we can help you if you give us the table schema and some sample entries.

    Is the understanding correct that you are looking for T-SQL code?

    • Edited by C0mmander Wednesday, April 2, 2014 12:08 PM
    • Marked as answer by Nitin gupts Wednesday, April 2, 2014 12:24 PM
    Wednesday, April 2, 2014 12:07 PM
  •  SqlCommand cmd1 = new SqlCommand("select last(Entry_time),last(Exit_time) from [Vehicle_movement]"

    i m using this statement but it giving error that last is not recognized built in function...

    Wednesday, April 2, 2014 12:16 PM