我有一个包含一些列的表。我想编写一个迭代遍历每一行的查询,查找与所选行中的列匹配的所有行的总数,并查找与2列匹配的所有行的计数。使用这两个值,我想找到百分比差异并将它们打印为column1,percentage(query1(column2)/ query2(column2和column3))。
以下是我写的查询
SELECT DISTINCT (t2.column1)
,(
SELECT count(DISTINCT column2)
FROM table1 t1
WHERE t1.column1 = t2.column1
ORDER BY column2
) AS total_count
,(
SELECT count(DISTINCT column2)
FROM table1 t1
WHERE t1.column1 = t2.column1
AND column3 IN (
10
,20
)
ORDER BY column1
,column2
,column3
) AS column3_count
FROM table1 t2;
以上查询有效,但需要花费大量时间来处理。
我希望它为
SELECT DISTINCT (column1)
,percentage(query1 that matches ALL rows WITH column1 / query2 that match ALL rows WITH column1
AND SOME other CONSTRAINT)
FROM TABLE t1
我也想优化上面的查询。请让我知道
由于
答案 0 :(得分:0)
我认为你只想要条件聚合。对于计数:
select t1.column1,
count(distinct column2) as num_column2,
count(distinct case when column3 in (10, 20) then column2 end) as num_column2_column3
from table1 t1
group by t1.column1;
我不了解百分比的计算方法,但似乎是基于这些数字。
答案 1 :(得分:0)
select t1.column1,
count(distinct column2) as num_column2,
count(distinct case when column3 in (10, 20) then column2 end) as num_column2_column3
from table1 t1
group by t1.column1;