我的专栏是这样的:
column1 | column2
--------+--------
100 | 01
100 | 01
101 | 02
101 | 03
102 | 04
102 | 05
103 | 06
104 | 07
104 | 07
我希望输出如下:
column1 | column2
--------+--------
101 | 02
101 | 03
102 | 04
102 | 05
答案 0 :(得分:1)
declare @T table
(
c1 int,
c2 int
)
insert into @T values
(100, 1),
(100, 1),
(101, 2),
(101, 3),
(102, 4),
(102, 5),
(103, 6),
(104, 7),
(104, 7)
;with C as
(
select C1,
C2,
count(*) over(partition by C1, C2) as D1,
count(*) over(partition by C1) as D2
from @T
)
select C1, C2
from C
where D1 = 1 and
D2 = 2
如果要在结果集中包含103
,可以使用:
;with C as
(
select C1,
C2,
count(*) over(partition by C1, C2) as D1
from @T
)
select C1, C2
from C
where D1 = 1
答案 1 :(得分:0)
尝试以下查询。用适当的表名替换表名。
SELECT Column1,Column2 FROM dbo.TableName GROUP BY Column1,Column2
HAVING count(*)<=1
请尝试
答案 2 :(得分:0)
试试这个
select myTable.column1,myTable.column2 from myTable,
(select column1 from
(select distinct column1, column2 from myTable where column2 )
group by column1 having count(column1) > 1) tempTable
where myTable.column1=tempTable.column1
order by myTable.column1,myTable.column2
答案 3 :(得分:0)
SELECT
column1,
column2
FROM atable
WHERE column1 IN (
SELECT column1
FROM atable
GROUP BY column1
HAVING COUNT(DISTINCT column2) > 1
)