簡體   English   中英

SQL 查詢以檢查列值的唯一性

[英]SQL query to to check uniqueness of a column value

需要查詢來檢查(選擇)colA 和 B 中的每個值組合是否在 col C 中具有唯一值。請幫助。 謝謝

這是我試過的查詢,它沒有給我想要的結果:

select unique CONCAT(A,B),C 
from tab1 
group by CONCAT(A,B),C 
having count(distinct (CONCAT(A,B)))>1;

樣品表:

A       B   C
Tim     123 1
Jill    123 1
Jill    456 2
John    456 1
Jill    456 1

這里第 3 行和第 5 行在 A 列和 B 列中具有相同的值,但在 C 列中具有不同的值,這是不正確的。 我需要 select 那些

是這樣的嗎? (第 1 - 7 行中的樣本數據;查詢從第 8 行開始):

SQL> with test (a, b, c) as
  2    (select 'Tim' , 123, 1 from dual union all
  3     select 'Jill', 123, 1 from dual union all
  4     select 'Jill', 456, 2 from dual union all
  5     select 'John', 456, 1 from dual union all
  6     select 'Jill', 456, 1 from dual
  7    )
  8  select t.*
  9  from test t
 10  where (t.a, t.b) in (select x.a, x.b
 11                       from test x
 12                       group by x.a, x.b
 13                       having count(distinct x.c) > 1
 14                      );

A             B          C
---- ---------- ----------
Jill        456          1
Jill        456          2

SQL>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM