日期:2014-05-16 浏览次数:20565 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-10 16:49:04
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[Bak]
if object_id('[Bak]') is not null drop table [Bak]
go
create table [Bak]([id] int,[name] varchar(2))
insert [Bak]
select 1000,'N1' union all
select 1001,'N2' union all
select 1002,'N3'
--> 测试数据:[Current]
if object_id('[Current]') is not null drop table [Current]
go
create table [Current]([id] int,[name] varchar(2))
insert [Current]
select 1000,'N1' union all
select 1000,'N9' union all
select 1001,'N2' union all
select 1002,'N3' union all
select 1002,'N4' union all
select 1002,'N5'
--------------开始查询--------------------------
select
a.id
from
(select id,count(1) as num from bak group by id) as a
inner join
(select id,count(1) as num from [Current] group by id) as b
on
a.id=b.id
and
b.num>a.num
----------------结果----------------------------
/* id
-----------
1000
1002
(2 行受影响)
*/