简体   繁体   English

如何在整个数据集中找到 column1 值超过 column1 平均值两倍的所有数据行?

[英]How to find all the data rows with column1 values more than twice the average column1 value across the dataset?

I am trying find all the data rows with column1 values more than twice the average column1 value across the dataset with the code below but it seems it's not correct data.我正在尝试使用下面的代码查找 column1 值超过整个数据集中 column1 平均值两倍的所有数据行,但它似乎不是正确的数据。 Here is the code:这是代码:

select *,column1 over() from databasetest.Table1
where column1 > (select 2*avg(column1) from databasetest.Table1)

And here is the sample output这是样本 output

在此处输入图像描述

As you can see, the column1 value is still less than the twice the average column1 value across the dataset.如您所见,column1 值仍然小于整个数据集中 column1 平均值的两倍。 I thought this should retrieve columns with more than twice the average column1 value across the dataset.我认为这应该检索数据集中 column1 平均值的两倍以上的列。 Did I do something wrong or I misunderstand something?我做错了什么或者我误解了什么? Any help would be appreciated任何帮助,将不胜感激

If I understand correctly, you can use window functions:如果我理解正确的话,你可以使用 window 函数:

select *
from (select t1.*, avg(column1) over () as avg_column1
      from databasetest.Table1 t1
     ) t1
where t1.column1 > 2 * avg_column1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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