[英]adding a second outer query in a subquery
I have a query below that gives me median, I want to put my count(*) that counts the total # of rows that I use in my median calculation in a seperate subquery (since it runs a bit better that way).我在下面有一个查询,它给了我中位数,我想把我的count(*)放在一个单独的子查询中,它计算我在中位数计算中使用的总行数(因为它运行得更好一些)。 What is the best way to do that?
最好的方法是什么? Thank you!
谢谢!
SELECT our_id, AVG(1.0 * our_val) as Median
FROM
( SELECT our_id, our_val,
COUNT(*) OVER (PARTITION BY our_id) AS cnt,
ROW_NUMBER() OVER (PARTITION BY our_id ORDER BY our_val) AS rn
FROM our_table
) AS x
WHERE rn IN ((cnt + 1)/2, (cnt + 2)/2) GROUP BY our_id;
You are calculating count(*)
in the subquery.您正在计算子查询中的
count(*)
。 Why not just use that?为什么不直接使用它?
SELECT our_id, AVG(1.0 * our_val) as Median, cnt
FROM (SELECT our_id, our_val,
COUNT(*) OVER (PARTITION BY our_id) AS cnt,
ROW_NUMBER() OVER (PARTITION BY our_id ORDER BY our_val) AS rn
FROM our_table
) x
WHERE rn IN ((cnt + 1)/2, (cnt + 2)/2)
GROUP BY our_id, cnt;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.