Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 是否可以在没有加入和联合的情况下完成此请求?_Sql_Oracle - Fatal编程技术网

Sql 是否可以在没有加入和联合的情况下完成此请求?

Sql 是否可以在没有加入和联合的情况下完成此请求?,sql,oracle,Sql,Oracle,假设有一个表a,其内容为: col1 col2 a 1 b 2 c 2 d 1 e 1 现在,是否可以编写一个select..from..而不使用join或union来获得col2中1的数量/col1中所有元素的数量(在本例中为3/5) 我在用甲骨文。感谢您的帮助 很抱歉,我没有在开头给出一个合适的示例。col1中的元素不一样 我会使用avg()和case: select avg(case when col2 = 1 then 1.0 e

假设有一个表a,其内容为:

col1   col2
a      1
b      2
c      2
d      1
e      1
现在,是否可以编写一个
select..from..
而不使用
join
union
来获得col2中1的数量/col1中所有元素的数量(在本例中为3/5)

我在用甲骨文。感谢您的帮助



很抱歉,我没有在开头给出一个合适的示例。col1中的元素不一样

我会使用
avg()
case

select avg(case when col2 = 1 then 1.0 else 0.0 end)
from t;
如果要为每个
col1
值设置此值,请输入
where
groupby

select col1, avg(case when col2 = 1 then 1.0 else 0.0 end)
from t
group by col1;

那么,您希望返回的唯一内容是“3,5”或0.6?做一个小组。使用case表达式进行条件聚合。
select  sum(case when col2 = 1 then 1 end) * 1.0 / count(distinct col1)
from    YourTable