【发布时间】:2015-03-13 08:03:52
【问题描述】:
我有一系列关于不同商家搜索查询的数据。 我有一个 python 脚本,它首先根据 1000、100 等计数(查询)实例从 qsql 中的主表创建头部、躯干和尾部查询集。
由于我的脚本运行的商家数量可能有/没有满足该阈值的查询,因此脚本不会记录始终生成的“head.csv”“torso.csv”..tail.csv。 如何通过尊重上述逻辑将查询分为头部、躯干和尾部组。
我还尝试使用 ntile 以百分位数 (33, 33, 33) 来划分组,但是如果商人的尾巴很长,那会使头部和躯干都倾斜。
当前:
# head
select trim(query) as query, count(*)
from my_merchant_table
-- other conditions & date range
GROUP BY trim(query)
having count(*) >=1000
#torso
select trim(query) as query, count(*)
from my_merchant_table
-- other conditions & date range
GROUP BY trim(query)
having count(*) <1000 and count(*) >=100
#tail
select trim(query) as query, count(*)
from my_merchant_table
-- other conditions & date range
GROUP BY trim(query)
having count(*) <100
# using ntile - but note that I have percentiles of "3" , 33.#% each, which introduces the skew
select trim(query), count(*) as query_count,
ntile(3) over(order by query_count desc) AS group_ntile
from my_merchant_table
group by trim(query)
order by query_count desc limit 100;
理想情况下,解决方案可以建立在此之上 -:
select trim(query), count(*) as query_count,
ntile(100) over(order by query_count desc) AS group_ntile
from my_merchant_table
-- other conditions & date range
group by trim(query)
order by query_count desc
这给了,
btrim query_count group_ntile
q0 1277 1
q1 495 1
q2 357 1
q3 246 1
# so on till group_ntile =100 , while the query_count reduces.
问题: 逻辑的最佳方式是什么,使整体逻辑商家不可知/不对配置进行硬编码?
注意:我在 Redshift 中获取数据,该解决方案应该特别兼容 postgres 8.0 和 redshift。
【问题讨论】:
-
这不是代码编写服务。请添加一些基础工作。
-
您要求定义合理的头尾组,但没有告诉我们您的特殊应用中的合理是什么意思。使用数据的分位数当然是一种方法。另一种方法是将躯干组定义为
mean +/- x*std。还有一千种其他方法。所以,在这里问之前,你必须问自己:你想达到什么目标?
标签: postgresql amazon-redshift