Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google bigquery bigquery group by并获取除groupby值之外的所有元素_Google Bigquery - Fatal编程技术网

Google bigquery bigquery group by并获取除groupby值之外的所有元素

Google bigquery bigquery group by并获取除groupby值之外的所有元素,google-bigquery,Google Bigquery,我正在处理bigquery中的一些事务历史记录。该表包含两列: 交易编号和项目id 我试图确定两个特征: 在同一笔交易中,有多少(普通和标准)产品与特定的商品id一起购买 在同一笔交易中,与特定项目id一起购买的产品列表是什么 例如:如果我们假设这些是在同一笔交易中购买的产品 |---------------------|------------------| | trans_num | item_id | |---------------------|--

我正在处理bigquery中的一些事务历史记录。该表包含两列:
交易编号
项目id

我试图确定两个特征:

  • 在同一笔交易中,有多少(普通和标准)产品与特定的
    商品id
    一起购买
  • 在同一笔交易中,与特定
    项目id
    一起购买的产品列表是什么
  • 例如:如果我们假设这些是在同一笔交易中购买的产品

    |---------------------|------------------|
    |      trans_num      |     item_id      |
    |---------------------|------------------|
    |          1          |         34       |
    |---------------------|------------------|
    |          1          |         35       |
    |---------------------|------------------|
    |          2          |         36       |
    |---------------------|------------------|
    |          2          |         37       |
    |---------------------|------------------|
    |          2          |         34       |
    |---------------------|------------------|
    
    我希望第一个输出是

    |----------------------|------------------|
    |      item_id         |     feature_1    |
    |----------------------|------------------|
    |          34          |         2.5      |
    |----------------------|------------------|
    |          35          |         2        |
    |----------------------|------------------|
    |          36          |         2        |
    |----------------------|------------------|
    |          37          |         2        |
    |----------------------|------------------|
    |          38          |         2        |
    |----------------------|------------------|
    
    功能_2
    应包含

    |--------|------------|
    |item_id | feature 2  |
    |--------|------------|
    | 34     |[35, 36, 37]|
    |--------|------------|
    | 35     | [34]       |
    |--------|------------|
    | 36     | [37, 34]   |
    |--------|------------|
    | 37     | [36, 34]   |
    |--------|------------|
    

    我应该如何处理这个问题?

    下面是针对BigQuery标准SQL的

    #standardSQL
    with pre_aggregation as (
      select a.trans_num, a.item_id, array_agg(b.item_id) other_items
      from `project.dataset.table` a
      join `project.dataset.table` b
      on a.trans_num = b.trans_num
      and a.item_id != b.item_id
      group by trans_num, item_id
      order by item_id, trans_num
    )
    select item_id,
      feature_1,
      array (
        select distinct item
        from t.feature_2 item
        order by item
      ) as feature_2
    from (
      select item_id,
        avg(array_length(other_items)) as feature_1,
        array_concat_agg(other_items) as feature_2
      from pre_aggregation
      group by item_id
    ) t     
    
    如果要应用于您问题中的样本数据

    `project.dataset.table` as (
      select 1 trans_num, 34 item_id union all
      select 1, 35 union all
      select 2, 36 union all
      select 2, 37 union all
      select 2, 34 
    )
    
    输出为


    很高兴这对你有用。请考虑投票表决。