【问题标题】:select thresholds dynamically from sql从 sql 中动态选择阈值
【发布时间】: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


【解决方案1】:

我想你从某种编程语言调用它的查询来处理信息。我在这方面的建议是获取所有记录并对其应用过滤器。考虑一下,如果您查询对数据有多个操作的数据库,这将导致应用程序的响应时间受到影响。

【讨论】:

    【解决方案2】:

    假设主要挑战是从值列表创建“图块”,这里有一些示例代码。它需要加拿大的 13 个省并将其分成所需数量的组。它使用省份名称,但数字也可以。

    SELECT * FROM Provinces ORDER BY province;  -- To see what we are working with
    +---------------------------+
    | province                  |
    +---------------------------+
    | Alberta                   |
    | British Columbia          |
    | Manitoba                  |
    | New Brunswick             |
    | Newfoundland and Labrador |
    | Northwest Territories     |
    | Nova Scotia               |
    | Nunavut                   |
    | Ontario                   |
    | Prince Edward Island      |
    | Quebec                    |
    | Saskatchewan              |
    | Yukon                     |
    +---------------------------+
    13 rows in set (0.00 sec)
    

    现在是代码:

    SELECT @n := COUNT(*),   -- Find total count (13)
           @j := 0.5,        -- 'trust me'
           @tiles := 3       -- The number of groupings
        FROM Provinces;
    
    SELECT group_start
        FROM (
            SELECT
                IF((@j * @tiles) % @n < @tiles, province, NULL) AS group_start,
                @j := @j + 1
            FROM Provinces
            ORDER BY province
             ) x
        WHERE group_start IS NOT NULL;
    
    +---------------------------+
    | group_start               |
    +---------------------------+
    | Alberta                   |
    | Newfoundland and Labrador |
    | Prince Edward Island      |
    +---------------------------+
    3 rows in set (0.00 sec)
    

    @tiles 设置为 4:

    +---------------+
    | group_start   |
    +---------------+
    | Alberta       |
    | New Brunswick |
    | Nova Scotia   |
    | Quebec        |
    +---------------+
    4 rows in set (0.00 sec)
    

    相当高效:1 次计算行数,1 次计算,1 次过滤掉非中断值。

    【讨论】:

      猜你喜欢
      • 2017-11-29
      • 2021-10-29
      • 1970-01-01
      • 2015-05-04
      • 2012-03-21
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多