[英]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.