日期:2014-05-17 浏览次数:20503 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-04-26 14:58:23
-- 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]([ROWID] varchar(5),[FGuid] uniqueidentifier,[Code] varchar(3),[Name] varchar(4),[Code1] varchar(3))
insert [huang]
select null,'04A88A44-622C-4517-8BAB-FCA4691C236F','002','北京','001' union all
select null,'04A88A44-622C-4517-8BAB-FCA4691C236F','002','北京','002' union all
select null,'04A88A44-622C-4517-8BAB-FCA4691C236F','002','北京','002' union all
select null,'04A88A44-622C-4517-8BAB-FCA4691C236F','002','北京','002' union all
select 'Guid1','04A88A44-622C-4517-8BAB-FCA4691C236F','002','北京','002' union all
select 'Guid2','04A88A44-622C-4517-8BAB-FCA4691C236F','002','北京','003'
--------------开始查询--------------------------
select rowid,fguid,code,NAME,MIN (code1)code1
from [huang]
GROUP BY rowid,fguid,code,NAME
----------------结果----------------------------
/*
rowid fguid code NAME code1
----- ------------------------------------ ---- ---- -----
NULL 04A88A44-622C-4517-8BAB-FCA4691C236F 002 北京 001
Guid1 04A88A44-622C-4517-8BAB-FCA4691C236F 002 北京 002
Guid2 04A88A44-622C-4517-8BAB-FCA4691C236F 002 北京 003
*/