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.
I have a table like this:PHSID FormNum Status1 3456 Y2 2156 <NULL>2 2157 YI need a view that will keep just one record per PHSID. The only values in the Status field are 'Y' and <NULL>. There can be between one and many records with the same PHSID value. This shouldn't be hard but it has me stumped.
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2009-06-22 : 11:43:51
Please define what "best" record means to you.delete sfrom table1 as sinner join (select phsid, min(formnum) as fm from table1 where status = 'u') AS xon x.phsid = s.phsid and x.formnum < s.formnumE 12°55'05.63"N 56°04'39.26"
Lamprey
Master Smack Fu Yak Hacker
4614 Posts
Posted - 2009-06-22 : 16:46:18
Maybe this:
SELECT T.PHSID, T.FormName, T.StatusFROM MyTable AS TINNER JOIN ( SELECT PHSID, MAX(FormName) AS FormName FROM MyTable WHERE Status = 'Y' GROUP BY PHSID ) AS D ON T.PHSID = D.PHSID AND T.FormName = D.FormName