繁体   English   中英

Oracle(或通用SQL)查询从简单表中获得2个独立的平均值和计数

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

将仅返回xy都不为空的行的值(示例数据中的第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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM