日期:2014-05-17 浏览次数:20774 次
select 字段1,字段2,字段3 from
(select 字段1,字段2,字段3,
row_number() over(partition by 字段1,字段2 order by 字段3) 'rn'
from [表名]) t
where t.rn=1
select *
from [huang] a
WHERE EXISTS (SELECT 1 FROM (SELECT [字段1],[字段2],MAX([字段3])[字段3] FROM huang GROUP BY [字段1],[字段2])b
WHERE a.[字段1]=b.字段1 AND a.[字段2]=b.字段2 AND a.[字段3]=b.[字段3])
select 字段1,字段2,min(字段3) as 字段3 from group by 字段1,字段2
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-11-05 14:43:40
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([字段1] int,[字段2] int,[字段3] int)
insert [huang]
select 1,1,2 union all
select 1,1,3 union all
select 2,2,3 union ALL
select 2,2,4
--------------开始查询--------------------------
select *
from [huang] a
WHERE EXISTS (SELECT 1 FROM (SELECT [字段1],[字段2],MIN([字段3])[字段3] FROM huang GROUP BY [字段1],[字段2])b
WHERE a.[字段1]=b.字段1 AND a.[字段2]=b.字段2 AND a.[字段3]=b.[字段3])
----------------结果----------------------------
/*
字段1 字段2 字段3
----------- ----------- -----------
1 1 2
2 2 3
*/
select 字段1,字段