如何计算postgres中一组查询字符串中整数的频率

时间:2016-11-21 13:40:51

标签: postgresql

我在postgres数据库中有一个列,它记录了我们网站上页面的搜索查询字符串。

该列包含

等数据
"a=2&b=4"
"a=2,3"
"b=4&a=3"
"a=4&a=3"

我想计算某个参数(a)的每个值的频率。

value | freq
------|------
3     | 3
2     | 2
4     | 1

无论如何要在单个SQL语句中执行此操作?

4 个答案:

答案 0 :(得分:3)

这样的事情:

def enrich(xRDD: RDD[xObject], yRDD: RDD[yObject], zRDD: RDD[zObject]): RDD[Result] = {
     functionB(functionA(xRDD, yRDD), zRDD)
}

在线示例:http://rextester.com/OXM67442

答案 1 :(得分:0)

尝试这样的事情:

select data_value,count(*) from (
  select data_name,unnest(string_to_array(data_values,',')) data_value from (
    select split_part(data_array,'=',1) data_name ,split_part(data_array,'=',2) data_values from (
          select unnest(string_to_array(mydata,'&')) data_array from mytable
         ) a
       ) b
    ) c where data_name='a' group by 1 order by 1

答案 2 :(得分:0)

假设保留计数的表名为paramcount

WITH vals(v) AS
   (SELECT regexp_replace(p, '^.*=', '')
    FROM regexp_split_to_table(
            'b=4&a=3,2',
            '&|,'
         ) p(p)
   )
INSERT INTO paramcount (value, freq)
   SELECT v, 1 FROM vals
ON CONFLICT (value)
   DO UPDATE SET freq = paramcount.freq + 1
      WHERE paramcount.value = EXCLUDED.value;

答案 3 :(得分:0)

  1. 在' a ='
  2. 之后获取csv整数
  3. 将其拆分为数字
  4. 统计值

    select v, count(*) from (
       SELECT c,unnest(string_to_array(unnest(regexp_matches(c,'a=([0-9,]+)','g')),',')) as v FROM qrs
    ) x group by v;
    
  5. 参数多态:

    WITH argname(aname) as (values ('a'::TEXT))
    select v, count(*) from (SELECT c,unnest(string_to_array(unnest(regexp_matches(c,aname||'=([0-9,]+)','g')),',')) as v FROM qrs,argname) x group by v;