
[英]Query to return row values which has same values in all the rows for each different value of one column
[英]Find row that has same value in one column over multiple rows while another column has different values
抱歉,我无法在SQLFiddle上进行演示,因为当我问这个问题时,它似乎已失效。
我有下表
Col1 Col2
==== ====
1 A
1 A
1 B
2 C
2 C
如果我通过查询运行分组
SELECT COL1,COL2
GROUPBY COL1,COL2
我会得到
Col1 Col2
==== ====
1 A
1 B
2 C
我的问题是,当第2列中有多个不同值时,如何编写查询以检测第1列的值? 从本质上讲,该查询的返回值应该给我“ 1”,因为这是在Col1中行具有相同的值,而Col2在多个数据行的范围内具有不同的值。
您可以将group by
子句与min()
和max()
:
select col1
from table t
group by col1
having min(col2) <> max(col2);
尝试这个:
select Col1,count(distinct Col2) as [different Col2 value count]
from yourtable
GROUP BY COL1
having count(distinct Col2)>1
SELECT DISTINCT t1.Col1
FROM table t1
JOIN table t2
ON T1.col1 = T2.col1 AND T1.col2 != T2.col2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.