August 18, 2010 at 9:13 pm
i have the following table
Employeeid Amount ReceivedDate
1 3000 2010-Jan-19
1 4000 2009-Dec-10
1 2500 2009-Mar-25
19 3400 2010-Apr-25
19 8700 2010-Jan-10
27 7700 2009-May-29
27 3400 2006-Jul-20
i need the following output from above table
(employee amount for max receivedDate)
Employeeid Amount ReceivedDate
1 3000 2010-Jan-19
19 3400 2010-Apr-25
27 7700 2009-May-29
how to achive this ?
August 18, 2010 at 9:41 pm
use a max() aggregate and a group by clause.
It's fairly simple, but it almost seems like this is homework. Try it yourself.
August 18, 2010 at 9:46 pm
I would suggest:
1. Using a Common Table Expression (CTE), get all the values of the table. Utilize the row_number function to assign row numbers to each row, starting at one for each employeeid, and ordering by the date descending, putting this value into a new column (RN).
2. Select the columns from the CTE where the new column RN = 1.
For assistance in figuring out how to use the row_number function, see this article[/url].
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 19, 2010 at 2:02 am
can you expline me how to use the simple query to achive this ?
August 19, 2010 at 2:09 am
Hi,
SELECT Employeeid,max(Amount),receiveddate from table
group by Employeeid,receiveddate.:-)
Varun R
August 19, 2010 at 6:54 am
varshini (8/19/2010)
can you expline me how to use the simple query to achive this ?
If people start giving you the solutions directly you will not learn. Wayne has given you the correct suggestion. Try some code yourself and if you are stuck somewhere, get back to us with the code that you have tried. We will be more than happy to help you.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
August 25, 2010 at 11:37 pm
SELECT EMPNAME,
AMOUNT,
RECIVEDDATE
FROM Table_Name A
WHERE RECIVEDDATE = (SELECT MAX(Reciveddate) FROM Table_Name B WHERE A.RecivedDate = B.RecivedDate)
Plz let me knw, whther this query solved your problem
August 26, 2010 at 10:23 am
You can use ROW_NUMBER() with PARTITION clause.
August 27, 2010 at 10:39 am
Hi ,
Try this script
Declare @t table(id int, amount varchar(10),adate datetime)
insert into @t select 1,'120', dateadd(day,-1,getdate())
insert into @t select 2,'121', getdate()
insert into @t select 1,'122', dateadd(day,-2,getdate())
insert into @t select 4,'123', getdate()
insert into @t select 1,'124', getdate()
insert into @t select 3,'125', dateadd(day,-3,getdate())
insert into @t select 3,'126', getdate()
--------HERE
;WITH cte AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY id ORDER BY adate desc) AS ROW,
id,
amount,
adate
FROM @t
)
SELECT *
FROM cte
where Row = 1
Thanks,
August 27, 2010 at 10:52 am
Hi, Change @t to your table name and your columns for id, amount, adate
WITH cte AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY id ORDER BY adate desc) AS ROW,
id,
amount,
adate
FROM @t
)
SELECT *
FROM cte
where Row = 1
this should work fine.
August 27, 2010 at 11:44 am
Same result as TakeITeasy, but using a derived table.
SELECT*
FROM(
SELECT ROW_NUMBER() OVER (PARTITION BY id ORDER BY adate desc) AS ROW,
id,
amount,
adate
FROM @t
) T
WHERE T.ROW = 1
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply