【问题标题】:Frequency of all combinations of values for certain column特定列的所有值组合的频率
【发布时间】:2020-06-24 13:23:48
【问题描述】:

我在 SQL Server 2012 中有一个数据集,其中包含 id 和 value 列,如下所示:

[id]  [value]
--------------
A        15
A        11
A        11
B        13
B        15
B        12
C        12
C        13
D        13  
D        12

我的目标是获取 [value] 的所有组合的频率计数,但有两个注意事项:

  1. 顺序无所谓,所以[11,12,15]不与[12,11,15]分开计算

  2. 重复值是分开计算的,所以[11,11,12,15]和[11,12,15]分开计算

  3. 我对任何长度的所有组合都感兴趣(不仅仅是对)

所以结果应该是这样的:

[combo]   [frequency]
---------------------
11,11,15  1
12,13,15  1
12,13     2

我见过here 涉及递归的答案,它们回答了类似的问题,但顺序很重要,而here 的答案涉及产生成对组合的自联接。这些都很接近,但我不太确定如何适应我的特定需求。

【问题讨论】:

  • 标记你的 sql 引擎。

标签: sql sql-server


【解决方案1】:

你可以使用string_agg():

select vals, count(*) as frequency
from (select string_agg(value, ',') within group (order by value) as vals, id
      from t
      group by id
     ) i
group by vals;

SQL Server 2012 不支持string_agg(),但您可以使用 XML hack:

select vals, count(*) as frequency
from (select id,
             stuff( (select concat(',', value)
                     from t t2
                     where t2.id = i.id
                     for xml path ('')
                    ), 1, 1, ''
                  ) as vals
      from (select distinct id from t) i
     ) i
group by vals;

【讨论】:

    【解决方案2】:

    您的数字字符串只是具有相同id 的所有值,按升序排列。所以我将最低的id 视为完整序列及其所有匹配项的规范名称。尽管您可以根据需要进行扩展,但这可以省去所有字符串操作。

    只需用计数器标记每个重复值,然后查找完全配对的组。

    with data as (
        select id, value,
            row_number() over (partition by id, value) as rn
    ), matches as (
        select l.id, r.id as match
        from data l full outer join data r on
             l.value = r.value and l.rn = r.rn and l.id <= r.id
        group by l.id
        having count(l.id) = count(*) and count(r.id) = count(*)
    )
    select id, count(match) as frequency
    from matches
    group by id;
    

    中间查询中的逻辑也很容易适应寻找共同值的子集。

    【讨论】:

      【解决方案3】:

      您可以使用 CTE 和 row_number 函数实现此目的。

      DECLARE @table table(id CHAR(1), val int)
      
      insert into @table VALUES
      ('A',15),
      ('A',11),
      ('A',11),
      ('B',13),
      ('B',15),
      ('B',12),
      ('C',12),
      ('C',13),
      ('D',13),  
      ('D',12);
      
      ;WITH CTE_rnk as
      (
      SELECT id,val, row_number() over (partition by id order by val) as rnk
      from @table
      ),
      CTE_concat as
      (
      SELECT id, cast(val as varchar(100)) as val, rnk
      from CTE_rnk 
      where rnk =1
      union all
      SELECT r.id, cast(concat(c.val,',',r.val) as varchar(100)) as val,r.rnk 
      from CTE_rnk as r
      inner join CTE_concat as c
      on r.rnk = c.rnk+1
      and r.id = c.id
      ),
      CTE_maxcombo as
      (
      SELECT id,val, row_number() over(partition by id order by rnk desc) as rnk
      from CTE_concat
      )
      select val as combo, count(*) as frequency 
      from CTE_maxcombo where rnk = 1
      group by val
      
      
      +----------+-----------+
      |  combo   | frequency |
      +----------+-----------+
      | 11,11,15 |         1 |
      |    12,13 |         2 |
      | 12,13,15 |         1 |
      +----------+-----------+
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-07
        相关资源
        最近更新 更多