如何查询vertica中聚合的百分比
how to query the percentage of aggregate in vertica
Table 产品
productId type
1 A
2 A
3 A
4 B
5 B
6 C
我想要的:
type perc
A 0.5
B 0.33
C 0.17
我们可以这样写一个简单的查询:
Select type, cnt/(select count(*) from product) AS perc
FROM (
select type, count(*) as cnt
from product
group by type
) nested
但是vertica不支持不相关的subselect
需要别人的帮助!
Vertica 支持相关和非相关子查询,即使您可能对连接谓词有限制。
因此,您上面的查询有效。而且 - 你猜怎么着 - 即使你使用缩进,它也会继续工作:
SQL> SELECT
type
, cnt/( select count (*) FROM product ) AS perc
FROM
( SELECT type, count (*) as cnt
FROM product
GROUP BY type
) nested ;
type | perc
------+----------------------
C | 0.166666666666666667
A | 0.500000000000000000
B | 0.333333333333333333
(3 rows)
当然你可以用不同的方式重写它。例如:
SQL> SELECT
a.type
, a.cnt/b.tot as perc
FROM
( SELECT type , count (*) as cnt
FROM product
GROUP BY type ) a
CROSS JOIN
( SELECT count (*) AS tot
FROM product ) b
ORDER BY 1
;
type | perc
------+----------------------
A | 0.500000000000000000
B | 0.333333333333333333
C | 0.166666666666666667
(3 rows)
你也可以使用解析函数,在这个应用程序中它很乱,但工作:
WITH product AS (
select 1 as productId, 'A' as type
union all select 2, 'A'
union all select 3, 'A'
union all select 4, 'B'
union all select 5, 'B'
union all select 6, 'C'
)
SELECT distinct /* distinct because analytic functions don't reduce row count like aggregate functions */
type, count(*) over (partition by type) / count(*) over ()
FROM product;
type | perc
------+----------------------
A | 0.500000000000000000
B | 0.333333333333333333
C | 0.166666666666666667
count(*) over (partition by type) 统计每种类型;
count(*) over () 计算所有内容,所以得到总计数
Table 产品
productId type
1 A
2 A
3 A
4 B
5 B
6 C
我想要的:
type perc
A 0.5
B 0.33
C 0.17
我们可以这样写一个简单的查询:
Select type, cnt/(select count(*) from product) AS perc
FROM (
select type, count(*) as cnt
from product
group by type
) nested
但是vertica不支持不相关的subselect
需要别人的帮助!
Vertica 支持相关和非相关子查询,即使您可能对连接谓词有限制。
因此,您上面的查询有效。而且 - 你猜怎么着 - 即使你使用缩进,它也会继续工作:
SQL> SELECT
type
, cnt/( select count (*) FROM product ) AS perc
FROM
( SELECT type, count (*) as cnt
FROM product
GROUP BY type
) nested ;
type | perc
------+----------------------
C | 0.166666666666666667
A | 0.500000000000000000
B | 0.333333333333333333
(3 rows)
当然你可以用不同的方式重写它。例如:
SQL> SELECT
a.type
, a.cnt/b.tot as perc
FROM
( SELECT type , count (*) as cnt
FROM product
GROUP BY type ) a
CROSS JOIN
( SELECT count (*) AS tot
FROM product ) b
ORDER BY 1
;
type | perc
------+----------------------
A | 0.500000000000000000
B | 0.333333333333333333
C | 0.166666666666666667
(3 rows)
你也可以使用解析函数,在这个应用程序中它很乱,但工作:
WITH product AS (
select 1 as productId, 'A' as type
union all select 2, 'A'
union all select 3, 'A'
union all select 4, 'B'
union all select 5, 'B'
union all select 6, 'C'
)
SELECT distinct /* distinct because analytic functions don't reduce row count like aggregate functions */
type, count(*) over (partition by type) / count(*) over ()
FROM product;
type | perc
------+----------------------
A | 0.500000000000000000
B | 0.333333333333333333
C | 0.166666666666666667
count(*) over (partition by type) 统计每种类型;
count(*) over () 计算所有内容,所以得到总计数