SQL 去重复
ROWID FGuid Code Name Code1
NULL04A88A44-622C-4517-8BAB-FCA4691C236F002北京001
NULL04A88A44-622C-4517-8BAB-FCA4691C236F002北京002
NULL04A88A44-622C-4517-8BAB-FCA4691C236F002北京002
NULL04A88A44-622C-4517-8BAB-FCA4691C236F002北京002
Guid104A88A44-622C-4517-8BAB-FCA4691C236F002北京002
Guid204A88A44-622C-4517-8BAB-FCA4691C236F002北京003
希望结果是Distinct去重复后
我想的得到的结果
ROWID FGuid Code Name Code1
NULL04A88A44-622C-4517-8BAB-FCA4691C236F002北京001
Guid104A88A44-622C-4517-8BAB-FCA4691C236F002北京002
Guid204A88A44-622C-4517-8BAB-FCA4691C236F002北京003
[解决办法]
----------------------------
-- 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
*/