首頁  >  文章  >  資料庫  >  sql 去除重复记录的语句

sql 去除重复记录的语句

WBOY
WBOY原創
2016-06-07 17:47:08961瀏覽

sql 去除重复记录的语句
表A:
ID,Name
表B:
ID,AID,Value

 

select case when a.name='ccc' then null else a.name end name,b.value from 表A a,表B b where a.ID=b.AID


select nullif(a.name,'ccc') name ,b.value from 表A a,表B b where a.ID=b.AID

生成测试数据表: [tb]

IF OBJECT_ID('[tb]') IS NOT NULL
    DROP TABLE [tb]
GO
CREATE TABLE [tb] ([name] [nvarchar](10),[value] [int])
INSERT INTO [tb]
SELECT 'aaa','1' UNION ALL
SELECT 'bbb','2' UNION ALL
SELECT 'ccc','3' UNION ALL
SELECT 'ccc','4' UNION ALL
SELECT 'ccc','5'

-->SQL查询如下:

SELECT name = CASE [value]
                   WHEN (
                            SELECT MIN([value])
                            FROM tb
                            WHERE name = t.name
                        ) THEN name
                   ELSE ''
              END, [value]
FROM [tb] t
/*
name       value
---------- -----------
aaa        1
bbb        2
ccc        3
           4
           5

(5 行受影响)

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn