how can i get latest records from the below table

  • I have a table where i am inserting into temp table ,i mean selecting the records from existing table.from this how can i get latest records.

    create table studentmarks

    (

    id int,

    name varchar(20),

    marks int

    )

    Insert into dbo.studentmarks values(1,'sha',20);

    insert into dbo.studentmarks values(1,'sha',30);

    insert into studentmarks values(2,'hu',30);

    insert into studentmarks values(2,'hu',40);

    select * from studentmarks

    create table #tmp2(studentname varchar(50),totalmarks int);

    insert into #tmp2

    select name as studentname,SUM(marks)as totalmarks from studentmarks group by name;

    select studentname,totalmarks from #tmp2;

    insert into studentmarks values(1,'sha',40)

    insert into studentmarks values(2,'hu',50)

    studentname totalmarks

    sha50

    hu70

    sha90

    hu70

    sha90

    hu70

    sha90

    hu120

    how to write a sql query to get the below output

    studentname totalmarks

    sha 90

    hu 120

  • Since that table has nothing to indicate any order of insert (eg dateinserted), there's no way to tell what the latest rows are (tables are unordered sets of rows). If that's something you need, add a datetime column and give it a default of getdate(), then you can use that in an order by or similar

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply