给出下表的2列:
c1 c2
------------
a1 | b1
a1 | b1
a2 | b2
a2 | b3
a3 | b3
我想从c2
列返回那些值,其中c2
列的值对于相同的c1
值会出现多次。我正在执行以下SQL查询以返回所需的结果:
SELECT DISTINCT ( c2 ) AS c
FROM ( SELECT c1 , c2 , COUNT (*) AS rowcount
FROM table
GROUP BY c1 , c2 HAVING rowcount > 1 )
结果:
c
---
b1
上述查询是否还有其他SQL语句?
答案 0 :(得分:1)
根据您的说明,您可以使用:
select distinct c1
from (select t.*, count(*) over (partition by c2) as cnt
from t
) t
where cnt >= 2;
基于您的示例结果
:select c1
from t
group by c1
having count(*) >= 2;
并基于修订的问题:
select c2
from t
group by c2
having count(*) >= 2;
答案 1 :(得分:0)
在has子句中使用count而不是使用子查询:-
select c1
from table
group by c1
having count(c2) > 1
答案 2 :(得分:0)
如果您希望c1中的所有值在表中出现一次以上(即使c2上的值相同),上述大多数答案将起作用。
如果只想测量可能在c2上具有多个DISTINCT值的c1值,则可以使用:
SELECT c1
FROM table
GROUP BY c1
HAVING COUNT(DISTINCT c2) > 1
答案 3 :(得分:-1)
SELECT c1
FROM your_table
GROUP BY c1
HAVING count(c2) > 1