[英]Oracle (or general sql) query to get 2 independent averages and counts from a simple table
我该怎么做:
SELECT myKey, avg(x), avg(y), count(x), count(y)
from t
where x is not null
and y is not null
group by myKEY
在桌子上
表t
MyKey,X,Y
K1,null,1
K2,null,1
K3,3,2
K4,4,3
K5,4,null
K6,4,null
基本上,我想尽可能在1个查询中获得2个平均值和2个计数(表很大)。 这是对观察值(X,Y)是独立的某些观察值进行一些分析。
但是平均值和计数是独立的,因为我需要从列中排除空值。
该查询:
SELECT myKey, avg(x), avg(y), count(x), count(y)
from t
where x is not null and y is not null
group by myKEY;
将仅返回x
和y
都不为空的行的值(示例数据中的第3和第4行)。
如果要从所有行获取值,只需删除where
子句:
SELECT myKey, avg(x), avg(y), count(x), count(y)
from t
group by myKEY;
avg()
和count()
函数将忽略NULL
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.