日期:2014-05-18 浏览次数:20767 次
-->生成测试数据 declare @tb table([col] nvarchar(2),[col2] nvarchar(2),[col3] nvarchar(2)) Insert @tb select N'aa',N'xx','' union all select N'aa',N'BB','' union all -- 如果有重复.... select N'aa',N'ww',N'zz' union all select N'bb',N'dd','' union all select N'cc',N'yy','' union all select N'cc',N'mm',N'zz' Select t.[col],min(t.[col2]) as [col2],t.[col3] from @tb t where col3 = (select case when max(col3)='zz' then 'zz' else '' end from @tb where [col] = t.[col]) group by t.[col],t.[col3] order by t.[col] /* col col2 col3 ---- ---- ---- aa ww zz bb dd cc mm zz (3 row(s) affected) */