次のようにすることもできます。
テストデータ
DECLARE @T TABLE(Col1 int, col2 VARCHAR(100), col3 VARCHAR(100),
col4 VARCHAR(100))
INSERT INTO @T
VALUES
(1, 'A', 'AA', 'AAA'),
(2, 'A', 'BB', 'AAA'),
(3, 'A', 'BB', 'AAA'),
(4, 'B', 'AA', 'AAA'),
(5, 'B', 'AA', 'BBB'),
(6, 'B', 'AA', 'CCC'),
(7, 'B', 'BB', 'AAA'),
(8, 'B', 'CC', 'AAA')
クエリ1
;WITH CTE
AS
(
SELECT
COUNT(Col1) OVER(PARTITION BY col2,col3,col4) AS Counts,
T.*
FROM
@T AS T
)
SELECT
*
FROM
CTE
WHERE
Counts>1
結果
2 2 A BB AAA
2 3 A BB AAA
クエリ2
;WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY col2,col3,col4 ORDER BY col1) AS RowNbr,
T.*
FROM
@T AS T
)
SELECT
*
FROM
CTE
WHERE
CTE.RowNbr>1
結果
2 3 A BB AAA