通过汇总一个表中的值来构造新表

时间:2020-09-01 20:51:52

标签: sql google-bigquery unpivot

我想基于一个表中的数据生成另一个表。

Customer  product_1      product_2     product_3
  cust_1    0               1            0
  cust_2    1               1            0
  cust_3    1               1            1

我有兴趣计算每种产品的购买率。

例如,product_1的购买价格为

(count of (where product_1 = 1)/(count of (where product_1 = 0) + count of (where product_1 = 1))) * 100 

assuming 1 = purchase, 0 = no purchase

i.e. 2/3 * 100 = 66.67

输出表应该如下-

 Products       Purchased_quantity      Not_purchased_quantity      Purchase_rate
product_1               2                        1                       66.67
product_2               3                        0                       100
product_3               1                        2                       33.33

2 个答案:

答案 0 :(得分:2)

以下内容适用于BigQuery Standard SQL,不需要硬编码的名称,而是全部动态地使用-对于任何[合理]个产品列...

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<ul id="menu">
</ul>

如果要应用于我们问题中的样本数据-输出为

#standardSQL
SELECT product,
  SUM(purchase) AS Purchased_quantity,
  SUM(1 - purchase) AS Not_purchased_quantity, 
  ROUND(100 * AVG(purchase), 2) AS Purchase_rate
FROM (
  SELECT 
    SPLIT(kv, ':')[OFFSET(0)] product,
    CAST(SPLIT(kv, ':')[OFFSET(1)] AS INT64) purchase
  FROM `project.dataset.table` t,
  UNNEST(SPLIT(REPLACE(TRIM(TO_JSON_STRING(t), '{}'), '"', ''))) kv
  WHERE SPLIT(kv, ':')[OFFSET(0)] != 'Customer'
)
GROUP BY product   

答案 1 :(得分:0)

您要取消透视和聚合。这是一种方法:

select product, sum(quantity), sum(1 - quantity),
       avg(quantity)
from ((select 'product1' as product, product1 as quantity from t) union all
      (select 'product2' as product, product2 as quantity from t) union all
      (select 'product3' as product, product3 as quantity from t) 
     ) p
group by product;

这假定列仅是0和1-可能更恰当地称为标记而不是数量。