使用 where 或 having 子句计算平均值
Calculating average by using where or having clause
我有一个 table T1 看起来像:
acctid time transactid amnt
233 xxx 2 400
我想return table 中amnt > average(amnt) 的所有交易。这是我的代码:
select * from (select transactid,amnt,avg(amnt)
over (partition by transactid) avgamnt from T1) where amnt>avg(mnt)
over (partition by transactid)
但是,我正在考虑另一个使用 having 子句的方法
select * from (select transactid,amnt,avg(amnt)
over (partition by transactid) avgamnt from T1) having amnt>avg(mnt)
over (partition by transactid)
请问哪一个更好(或正确),为什么?
I want to return all the transaction with amnt > the average(amnt) in
the table.
所以为什么不使用这个:
select * from T1 where T1.amnt > (select avg(t.amnt) from T1 t)
您的查询即将完成:
select *
from (select transactid, amnt,
avg(amnt) over (partition by transactid) as avgamnt
from T1
) t
where amnt > avgamnt;
您在子查询中定义列。然后,你只需要使用它。
我有一个 table T1 看起来像:
acctid time transactid amnt
233 xxx 2 400
我想return table 中amnt > average(amnt) 的所有交易。这是我的代码:
select * from (select transactid,amnt,avg(amnt)
over (partition by transactid) avgamnt from T1) where amnt>avg(mnt)
over (partition by transactid)
但是,我正在考虑另一个使用 having 子句的方法
select * from (select transactid,amnt,avg(amnt)
over (partition by transactid) avgamnt from T1) having amnt>avg(mnt)
over (partition by transactid)
请问哪一个更好(或正确),为什么?
I want to return all the transaction with amnt > the average(amnt) in the table.
所以为什么不使用这个:
select * from T1 where T1.amnt > (select avg(t.amnt) from T1 t)
您的查询即将完成:
select *
from (select transactid, amnt,
avg(amnt) over (partition by transactid) as avgamnt
from T1
) t
where amnt > avgamnt;
您在子查询中定义列。然后,你只需要使用它。