欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

sql 去除重复记录的语句

程序员文章站 2022-05-15 15:45:57
...
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 行受影响)