数据库环境:SQL SERVER 2008R2
Sql Server有提供求集合差集的函数——EXCEPT。先看看EXCEPT的用法,
{ <query_specification> | ( <query_expression> ) }
{ EXCEPT }
{ <query_specification> | ( <query_expression> ) } 从 EXCEPT 操作数左边的查询中返回右边的查询未返回的所有非重复值。
上面是摘自MSDN对EXCEPT函数的用法介绍。 在这里,我们的要求有点特别,集合B中存在多少条集合A的记录,那么,在集合A中剔除集合B中对应的记录条数。
假如A表有数据如下:
id name
1 a
1 a
2 b B表数据如下:
id name
1 a
3 c 根据需求,B表中有一条记录和A表有重复,因此,在A表中,把该重复记录的一条去掉,
结果数据如下:
id name
1 a
2 b
需求已经清晰了,现在开始来实现实现的方法是:分别给a表和b表的重复记录编号,
只要在b表中存在和a表编号、id、name一样的记录,即在a表进行过滤。
先准备基础数据
WITH a
AS ( SELECT 1 AS id ,
'a' AS NAME
UNION ALL
SELECT 1 AS id ,
'a' AS NAME
UNION ALL
SELECT 2 AS id ,
'b' AS NAME
UNION ALL
SELECT 3 AS id ,
'c' AS NAME
UNION ALL
SELECT 3 AS id ,
'c' AS NAME
UNION ALL
SELECT 1 AS id ,
'a' AS NAME
UNION ALL
SELECT 4 AS id ,
'd' AS NAME
),
b
AS ( SELECT 3 AS id ,
'c' AS NAME
UNION ALL
SELECT 1 AS id ,
'a' AS NAME
UNION ALL
SELECT 2 AS id ,
'b' AS NAME
UNION ALL
SELECT 3 AS id ,
'c' AS NAME
UNION ALL
SELECT 1 AS id ,
'a' AS NAME
)
分别来看一下a表和b表的数据
第一种方式,用NOT EXISTS来实现
SELECT id ,
NAME
FROM ( SELECT id ,
ROW_NUMBER() OVER ( PARTITION BY id, NAME ORDER BY id ) AS nid ,
NAME
FROM a
) a
WHERE NOT EXISTS ( SELECT NULL
FROM ( SELECT id ,
ROW_NUMBER() OVER ( PARTITION BY id,
NAME ORDER BY id ) AS nid ,
NAME
FROM b
) b
WHERE b.nid = a.nid
AND b.id = a.id
AND b.NAME = a.NAME )
第二种实现方式,通过EXCEPT来实现
SELECT id ,
NAME
FROM ( SELECT id ,
ROW_NUMBER() OVER ( PARTITION BY id, NAME ORDER BY id ) AS nid ,
NAME
FROM a
EXCEPT
SELECT id ,
ROW_NUMBER() OVER ( PARTITION BY id, NAME ORDER BY id ) AS nid ,
NAME
FROM b
) a
方法1和方法2本质上是一样的思路,只不过写法不同而已。
我们来看下结果
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFUAAABGCAIAAAAy1U84AAAC10lEQVR4nO2YsY6cMBCG551QCvMu0SmF/SB5AIvW3Rana0IqSzxEQEqLRJ27bU5i0y6SU3gXbDBwp5hZkP3LugUDN/78D2AGlFflef5mS4iqLEshKlHd2un0rJRSLFEsKcsyYYkQVZKwN5cAwLk96tEboxOcnXmet4bAO/+rLSGqhLGnp6eEMd00/8uvVlSVEJUQ1en0nCTsdU0AMNrVPWY/2Jpehc0/laadttULvWhz/j/71ub8x5Jn/sMJfvyUITfouu58uXa4wo84p8gf+SP/2mhqTgiv/UV1RKw5ASPGKKKkMDoKAFRa+/q4PmRoOMslF/+UFonfYLQjSgqU0qGj5oQQYp4MBv9nhron/wnn9G6XFbHmhMpOUnt2eL8vKaGUeOPv/4WZSyj8tRnapKXS6tKb9wmRlHDpuvADWuCvObm7ISkSv071CawextBnTZako4n7xO2/wD9yAIu/G/GMgCyTa06o1BPmTJwPaIf8Xc2JdT9bz3nCa/uX2DfHFvk/ejf9v5b59esOLNLJ0IZsp7LbhN9IPfNl40Nr/P2MTx48GnjhDT15/y8PPK7/In/kD5j/fLmG3OBWBgVA/rsTQdd1bdue3/9iNoBd1F0HfgDA5G/b9tHsSpn80f/o/2zLv8K3lyD9//39i15Ee+aP/h/D/6345/wv2PDdxgpE/t3532RpmjVo/DvxXzVZ2icAJv8+/C9YDx2k/wXr73pjKjD49+G/lf6o/HH9hwe/4D+yov/R//PlGrT/QyUoyBLYLf/xJx45olORP/JH/nV+74vxdX7k75+VofhejB+Iv8nSlDHPgzkOf8GAFd4HMxvxUfWPmRNuH+RY/E2W3gsAyN//zsM9NhK/GWYH+W8VI/zm4yH43QPzFXgmzi3/myx9fP6b48J+/qXpjvzfIjByRKcif+QPnV/Xv/ADI0d0qjXrXwFKKbWLOuwDFfnDVuQPW6Hz/wMYmZJZV6QB7wAAAABJRU5ErkJggg==" alt="" />
(本文完)