Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi all,i have this table called ProfileRecID ModID ProfileName-------------------------------------------------------1 5 My Account Management2 5 My Account Management3 5 My Account Management4 7 My Profile5 7 My Profile6 7 My ProfileI would like to return the min value for RecID for each ProfileName.RecID ModID ProfileName---------------------------------1 5 My Account Management4 7 My ProfileI am not very good in SQL statement , please help thanks.kt
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2007-08-26 : 08:09:57
[code]SELECT p.RecID, p.ModID, p.ProfileNameFROM Profile pINNER JOIN ( SELECT RecID = MIN(RecID), ProfileName FROM Profile GROUP BY ProfileName ) m ON p.RecID = m.RecID[/code]KH[spoiler]Time is always against us[/spoiler]
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2007-08-26 : 10:38:49
Or the SQL Server 2005 approach. If you have many records this may be faster.Compare them both in SSMS and SQL Profiler.
SELECT RecID, ModID, ProfileNameFROM (SELECT RecID, ModID, ProfileName, ROW_NUMBER() OVER (PARTITION BY ProfileName ORDER BY RecID) AS RowIDFROM Profile) AS dWHERE RowID = 1