日期:2014-05-17 浏览次数:20476 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-01-18 16:25:20
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([Id] int,[A] int,[B] int)
insert [huang]
select 101,1,1 union all
select 101,1,2 union all
select 101,1,3 union all
select 101,2,1 union all
select 101,2,2
--------------开始查询--------------------------
SELECT id,a,MAX(b)b
FROM huang a
WHERE EXISTS (SELECT 1 FROM (
select id,MAX(a)a
from [huang]
GROUP BY id)b WHERE a.id=b.id AND a.a=b.a)
GROUP BY id,a
----------------结果----------------------------
/*
id a b
----------- ----------- -----------
101 2 2
(1 行受影响)
*/