我有一个sql查询,我只想显示特定字段有双重条目的那些记录。
以下是我当前输出的示例:
Test 1 Test2 Test3
1 12 654
2 12 4655
3 15 65987
4 16 3548
5 14 7348
6 14 365
7 17 987
8 L13 6547
9 L13 98687
我想仅提取以下内容(仅限Test2的计数为>且为数字的那些):
Test 1 Test2 Test3
1 12 654
2 12 4655
5 14 7348
6 14 365
我目前的查询是:
SELECT *
FROM Test
WHERE (Test4 BETWEEN @startdate AND @enddate) AND (isnumeric(test2) = 1)
Group by Test1
having Count(Distinct(test2)) > 1
ORDER BY Test2, test3
答案 0 :(得分:2)
使用子查询进行计数和选择:
Select * From test
where test2 in (
Select test2 From (
Select test2, count(*) C from test
where (Test4 BETWEEN @startdate AND @enddate) AND (isnumeric(test2) = 1)
group by test2
having count(*) > 1 ) f
)
答案 1 :(得分:0)
;WITH CTE AS
(
SELECT *,
N = COUNT(*) OVER(PARTITION BY Test2)
FROM YourTable
WHERE ISNUMERIC(Test2) = 1
)
SELECT *
FROM CTE
WHERE N > 1;