22,209
社区成员
发帖
与我相关
我的任务
分享
SELECT migs.group_handle, mid.*
FROM sys.dm_db_missing_index_group_stats AS migs
INNER JOIN sys.dm_db_missing_index_groups AS mig
ON (migs.group_handle = mig.index_group_handle)
INNER JOIN sys.dm_db_missing_index_details AS mid
ON (mig.index_handle = mid.index_handle)
DECLARE @n INT ---传入n的数值
SET @n = 3
;WITH test AS(
SELECT bundle,COUNT(1) AS num
FROM @table
GROUP BY bundle
HAVING COUNT(1) = @n
)SELECT * FROM @table a JOIN test b ON a.bundle = b.bundle
WHERE EXISTS(SELECT 1 FROM @table c WHERE c.bundle = a.bundle AND c.section <>a.section)
DECLARE @table TABLE
(id INT PRIMARY KEY,bundle NVARCHAR(20),section NVARCHAR(20))
INSERT INTO @table VALUES(1,'001','A')
INSERT INTO @table VALUES(2,'001','A')
INSERT INTO @table VALUES(3,'002','A')
INSERT INTO @table VALUES(4,'002','B')
INSERT INTO @table VALUES(5,'002','B')
INSERT INTO @table VALUES(6,'003','B')
INSERT INTO @table VALUES(7,'003','A')
INSERT INTO @table VALUES(8,'003','C')
INSERT INTO @table VALUES(9,'004','A')
INSERT INTO @table VALUES(10,'005','D')
INSERT INTO @table VALUES(11,'005','A')
select *
from @table t ,(select bundle from @table group by bundle having count(*)>=3)s
where t.bundle=s.bundle
and exists(select 1 from @table where bundle=t.bundle and section!=t.section)
/*
id bundle section bundle
----------- -------------------- -------------------- --------------------
3 002 A 002
4 002 B 002
5 002 B 002
6 003 B 003
7 003 A 003
8 003 C 003
(6 個資料列受到影響)
*/
--可以修改成这样的。试试看
SELECT *
from
@table t
WHERE
exists(select bundle from @table WHERE bundle=t.bundle group by bundle having count(1)>=2)
and
exists(select 1 from @table where bundle=t.bundle and section<>t.section)
DECLARE @table TABLE
(id INT PRIMARY KEY,bundle NVARCHAR(20),section NVARCHAR(20))
INSERT INTO @table VALUES(1,'001','A')
INSERT INTO @table VALUES(2,'001','A')
INSERT INTO @table VALUES(3,'002','A')
INSERT INTO @table VALUES(4,'002','B')
INSERT INTO @table VALUES(5,'002','B')
INSERT INTO @table VALUES(6,'003','B')
INSERT INTO @table VALUES(7,'003','A')
INSERT INTO @table VALUES(8,'003','C')
INSERT INTO @table VALUES(9,'004','A')
INSERT INTO @table VALUES(10,'005','D')
INSERT INTO @table VALUES(11,'005','A')
--SELECT * FROM @table
select
*
from
@table t
where
bundle in(select bundle from @table group by bundle having count(1)>=2)
and
exists(select 1 from @table where bundle=t.bundle and section<>t.section)
/*id bundle section
----------- -------------------- --------------------
3 002 A
4 002 B
5 002 B
6 003 B
7 003 A
8 003 C
10 005 D
11 005 A
(8 行受影响)
*/