日期:2014-05-17 浏览次数:20571 次
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[RuleID] nvarchar(3),[MatchNum] int,[Frequency] int)
Insert #T
select 1,N'001',1,3 union all
select 2,N'001',2,5 union all
select 3,N'002',3,1 union all
select 4,N'002',4,10 union all
select 5,N'002',1,6
Go
SELECT *
FROM
(Select *,COUNT(*)OVER(PARTITION BY [RuleID]) con,ROW_NUMBER()OVER(PARTITION BY [RuleID] ORDER BY [Frequency] desc) AS row from #T AS a )t
WHERE row=1
/*
ID RuleID MatchNum Frequency con row
2 001 2 5 2 1
4 002 4 10 3 1
*/
select * from tb a
where id=(select top 1 id from tb where RuleID=a.RuleID order by MatchNum desc,Frequency desc)