Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 selectmax

Author  Topic 

enniwesw
Starting Member

8 Posts

Posted - 2010-04-14 : 08:59:50
Hello ladies and gentlemen,

given a table with data like:

Table 1 contains data like:-
whs product dated mv_ref
01 4510 2010/03/25 TS67552/1
01 4510 2010/04/08 TS68083
01 4520 2010/03/23 TS67552
01 4520 2010/04/12 TS68200
01 4524 2010/03/23 TS67534
01 4610 2010/03/19 TS67523
01 4610 2010/03/19 TS67523
01 4610 2010/03/26 CN311368
01 4610 2010/04/06 TS64685/1
01 5202 2010/03/04 TS66832
01 5202 2010/03/08 CN311261
01 5202 2010/04/08 TS68083
01 9131 2010/03/05 TS66757
01 110004 2010/03/10 TS66012
01 110004 2010/03/10 TS66012


need a script which will extract a single mv_ref for a product having maximum date....

Sachin.Nand

2937 Posts

Posted - 2010-04-14 : 09:04:09
[code]
select * from(
select *,row_number()over(partition by product order by dated desc) as rowid from yourtable
)T where rowid=1
[/code]

PBUH
Go to Top of Page

enniwesw
Starting Member

8 Posts

Posted - 2010-04-14 : 09:24:21
what i am looking for are products, and the most resent mv_ref….
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-04-14 : 09:32:05
Please post some sample expected output.

PBUH
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-04-14 : 09:33:45
[code]
declare @tbl as table(whs int, product int, dated datetime, mv_ref varchar(50))
insert into @tbl
select 01 ,4510 ,'2010/03/25','TS67552/1'union all
select 01 ,4510 ,'2010/04/08',' TS68083' union all
select 01 ,4520 ,'2010/03/23',' TS67552'union all
select 01 ,4520 ,'2010/04/12',' TS68200'union all
select 01 ,4524 ,'2010/03/23',' TS67534'union all
select 01 ,4610 ,'2010/03/19',' TS67523'union all
select 01 ,4610 ,'2010/03/19',' TS67523'union all
select 01 ,4610 ,'2010/03/26',' CN311368'union all
select 01 ,4610 ,'2010/04/06',' TS64685/1'union all
select 01 ,5202 ,'2010/03/04',' TS66832'union all
select 01 ,5202 ,'2010/03/08',' CN311261'union all
select 01 ,5202 ,'2010/04/08',' TS68083'union all
select 01 ,9131 ,'2010/03/05',' TS66757'union all
select 01 ,110004 ,'2010/03/10',' TS66012'union all
select 01 ,110004 ,'2010/03/10',' TS66012'

select * from @tbl

select * from(
select *,row_number()over(partition by product order by dated desc) as rowid from @tbl
)T where rowid=1


--output
whs product dated mv_ref rowid
----------- ----------- ----------------------- -------------------------------------------------- --------------------
1 4510 2010-04-08 00:00:00.000 TS68083 1
1 4520 2010-04-12 00:00:00.000 TS68200 1
1 4524 2010-03-23 00:00:00.000 TS67534 1
1 4610 2010-04-06 00:00:00.000 TS64685/1 1
1 5202 2010-04-08 00:00:00.000 TS68083 1
1 9131 2010-03-05 00:00:00.000 TS66757 1
1 110004 2010-03-10 00:00:00.000 TS66012 1



[/code]
Whats wrong with the above output?

PBUH
Go to Top of Page

enniwesw
Starting Member

8 Posts

Posted - 2010-04-15 : 02:00:57
Many thanks! that's what i am talking about...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-15 : 08:37:55
Also see how you can effectively make use of row_number() function for various purposes
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -