SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 highest id and correct value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pnpsql
Posting Yak Master

India
246 Posts

Posted - 03/19/2013 :  11:46:51  Show Profile  Reply with Quote
hi team ,

i have table with follwoing data
create table test1 (id numeric(10) , date1 datetime , total numeric(10))
insert into test1
values(1,'14-mar-2013',100),
(2,'14-mar-2013',110),
(3,'14-mar-2013',10),
(4,'15-mar-2013',110),
(5,'15-mar-2013',120),
(6,'15-mar-2013',70)

and i want to get maximum id for each date with correct total

like
3 2013-03-14 00:00:00.000 10
6 2013-03-15 00:00:00.000 70


select max(id), date1, max(total)
from test1
group by date1
it gives wrong record

challenge everything

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/19/2013 :  11:49:28  Show Profile  Reply with Quote

SELECT id,date1,total
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY date1 ORDER BY id DESC) AS Seq,*
FROM test1
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8768 Posts

Posted - 03/19/2013 :  11:51:25  Show Profile  Visit webfred's Homepage  Reply with Quote
select t1.* from test1 as t1
join
(select max(id) as id, date1 from test1 group by date1)t2
on t1.id = t2.id


Too old to Rock'n'Roll too young to die.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000