【问题标题】:SQL: calculate the percentage overlapSQL:计算重叠百分比
【发布时间】:2021-12-17 09:15:00
【问题描述】:

** 这个问题之前在这里被问过:BigQuery Calculating Percent Overlap Between Values Multiple Columns 我正在尝试重做一件非常相似的事情,但给出的答案最终对我不起作用***

我有一个如下所示的表格:

category_name  |   item_id
---------------|------------
category1      |  item1
category2      |  item1
category3      |  item1
category1      |  item2
category4      |  item2
category1      |  item3
category5      |  item3
category5      |  item2
category6      |  item4
category3      |  item5
category3      |  item6
category1      |  item6
category2      |  item5
category1      |  item4

我试图实现的第一件事是创建一个 6x6 表,显示每对之间的重叠计数,我使用以下代码成功地做到了:

select t.category_name,
       countif( t2.category_name = 'category1' ) as category1,
       countif( t2.category_name = 'category2' )  as category2,
       countif( t2.category_name = 'category3' )  as category3,
       countif( t2.category_name = 'category4' )  as category4,
       countif( t2.category_name = 'category5' ) as category5
from t join
     t t2
     on t.item = t2.item
group by t.category_name;

但是,我还想计算重叠百分比而不是计数。因此,我尝试在 select 子句中的每一行末尾添加“/count(*)”来获取 overlpa 的百分比,如下所示:

select t.category_name,
       countif( t2.category_name = 'category1' ) / count(*) as category1,
       countif( t2.category_name = 'category2' ) / count(*) as category2,
       countif( t2.category_name = 'category3' ) / count(*) as category3,
       countif( t2.category_name = 'category4' ) / count(*) as category4,
       countif( t2.category_name = 'category5' ) / count(*) as category5
from t join
     t t2
     on t.item = t2.item
group by t.category_name;

出现的问题是,对于具有相同 category_name 的行和列,重叠的百分比最终不是 100%。例如,对于类别 1 与类别 1,该百分比最终小于 50%。如果您发现我的代码有任何问题,您能告诉我吗?

这是我要找的表格:

category_name  | category1   |  category2  |   category3   |   category4   |  category5  |  category6
--------------------------------------------------------------------------------------------------------
category1      |   100%      |     20%     |      40%      |      20%      |     40%     |     20%
category2      |    50%      |     100%    |      100%     |       0%      |      0%     |     0%
category3      |    66.67%   |     66.67%  |      100%     |       0%      |      0%     |     0%
category4      |   100%      |      0%     |       0%      |      100%     |     100%    |     0%
category5      |   100%      |      0%     |       0%      |       50%     |     100%    |     0%
category6      |   100%      |      0%     |       0%      |       0%      |      0%     |    100%

【问题讨论】:

  • 不清楚您期望什么结果 - 请提供与您的问题中的输入数据匹配的预期输出,以便我们可以帮助您:o)
  • @MikhailBerlyant 刚刚做到了!谢谢

标签: sql database google-bigquery


【解决方案1】:

考虑以下方法

with temp as (
  select category_name, array_agg(item) items
  from your_table
  group by category_name
) 
select * from (
  select t1.category_name, t2.category_name category,
    round(100 * (select count(*) from t1.items item join t2.items item using(item)) / array_length(t1.items), 2) overlap
  from temp t1, temp t2
)
pivot (min(overlap) for category in ('category1', 'category2', 'category3', 'category4', 'category5', 'category6'))          

如果应用于您问题中的样本数据 - 输出是

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多