我有以下数据。
ColumnA ColumnB
1234 1
1234 2
1234 3
1235 2
1236 2
1237 1
1237 2
1238 1
1238 3
1238 5
我需要输出如下所示,它只显示列中1个具有相同值的项目(例如ColumnA)
ColumnA ColumnB
1234 1
1234 2
1234 3
1237 1
1237 2
1238 1
1238 3
1238 5
实际上我通过下面的查询获得了第一个表,如何根据最终表格对其进行修改?
select ColumnA, ColumnB
from TableX
group by ColumnA, ColumnB
--having COUNT(ColumnA) > 1
--having COUNT(ColumnB) > 0
order by ColumnA asc
更新
在查询下方运行会在下面的表格中显示,但如果我可以按照原始请求运行,那将会很棒...
SELECT b.ColumnA
(
select ColumnA, ColumnB
from TableX
group by ColumnA, ColumnB
--having COUNT(ColumnA) > 1
--having COUNT(ColumnB) > 0
-- order by ColumnA asc
) b
group by b.ColumnA
having COUNT(b.ColumnA) > 1
结果查询是:
ColumnA
1234
1237
1238
更新
就是这样!如果还有其他解决方案,请告诉我......
SELECT c.*
FROM TableX c
WHERE c.ColumnA IN
(
SELECT b.ColumnA
(
select ColumnA, ColumnB
from TableX
group by ColumnA, ColumnB
--having COUNT(ColumnA) > 1
--having COUNT(ColumnB) > 0
-- order by ColumnA asc
) b
group by b.ColumnA
having COUNT(b.ColumnA) > 1
)
答案 0 :(得分:0)
如果我的理解是正确的,您只想在columnA值重复时输出行:
declare @tableX table
(
ColumnA int,
columnB int
)
insert into @tableX
values
(1234, 1 ),
(1234, 2 ),
(1234, 3 ),
(1235, 2 ),
(1236, 2 ),
(1237, 1 ),
(1237, 2 ),
(1238, 1 ),
(1238, 3 ),
(1238, 5 )
select ColumnA, columnB
from @tableX x
where exists (select 1 from @tableX where ColumnA = x.ColumnA group by ColumnA having COUNT(*) > 1);