[英]How to select the value that exist in every single row sql
我需要检查表“List”的每一行的“nS”列的值是否相同,然后显示该值。
Example01 :这应该返回“S1”,因为每一行在“nS”列中都有该值。
| nS | nE |
|:-----|-----:|
| 'S1' | 'A1' |
| 'S1' | 'A2' |
| 'S1' | 'A3' |
| 'S1' | 'A4' |
Example02 :这不应返回任何内容,因为 'nS' 的值不同。
| nS | nE |
|:-----|-----:|
| 'S1' | 'A1' |
| 'S2' | 'A2' |
| 'S1' | 'A3' |
| 'S3' | 'A4' |
到目前为止,我做了下面的代码,但它不起作用,因为 GROUP BY 语句。
SELECT nS FROM List GROUP BY nS HAVING count(nS) = count(*);
像这样的东西?
SQL> with test (ns, ne) as
2 (select 's1', 'a1' from dual union all
3 select 's1', 'a2' from dual union all
4 select 's1', 'a3' from dual union all
5 select 's1', 'a4' from dual
6 )
7 select distinct ns
8 from test
9 where 1 = (select count(distinct ns) From test);
NS
--
s1
SQL>
SQL> with test (ns, ne) as
2 (select 's1', 'a1' from dual union all
3 select 's2', 'a2' from dual union all
4 select 's1', 'a3' from dual union all
5 select 's3', 'a4' from dual
6 )
7 select ns
8 from test
9 where 1 = (select count(distinct ns) From test);
no rows selected
SQL>
在您的查询中,您必须将COUNT(*)
替换为返回表中总行数的子查询:
SELECT nS
FROM List
GROUP BY nS
HAVING COUNT(*) = (SELECT COUNT(*) FROM List);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.