简体   繁体   English

根据条件从一列中提取数据并存储在另一列中

[英]Extracting data from one column and storing in another based on a condition

The data I have looks like below-我的数据如下所示-

ID      category     Type_1     Type_2     Match?      Quantity
123       1            A          B       no match        2
123       2            A          B       no match        1
123       3            A          B       no match        6
123       4            A          B       no match        6
123       5            A          B       no match        9
123       6            A          B       no match        1
456       1            A          A        match          6
456       2            A          A        match          4
456       3            A          A        match          4
456       4            A          A        match          3
456       5            A          A        match          0
456       6            A          A        match          1

I want to restructure this table by doing the following-我想通过执行以下操作来重组此表 -

1) When the category is 3 , I want to create another column cat_3 and store the Type_2 for category 3 underneath that. 1)当类别为 3时,我想创建另一列cat_3并将类别 3Type_2存储在其下方。 Create another column Quantity_3 and store the quantity corresponding to 3 underneath that.创建另一列Quantity_3并在其下方存储与 3 对应的数量。

2) For all other categories other than 3 , I want to create another column Other_categories and store Type_1 corresponding to other categories underneath that. 2)对于除 3 以外的所有其他类别,我想创建另一列Other_categories并存储Type_1对应于其下方的其他类别 Create another column Quantity_for_other_categories and store the sum of quantities corresponding to all other categories underneath that.创建另一列Quantity_for_other_categories并在其下存储与所有其他类别对应的数量总和

The result should look like below-结果应如下所示 -

ID    cat_3     Quantity_3     Other_categories   Quantity_for_other_categories    Match?
123     B           6                 A                       19                   no match
456     A           4                 A                       14                   match

Below is for BigQuery Standard SQL以下是 BigQuery 标准 SQL

#standardSQL
SELECT id, cat_3, Quantity_3, Other_categories, Quantity_for_other_categories, Match
FROM (
  SELECT id, 
    Type_2 AS cat_3,
    Quantity AS Quantity_3,
    Match
  FROM `project.dataset.table`
  WHERE category = 3
  ORDER BY id
) FULL OUTER JOIN (
  SELECT id, 
    Type_1 AS  Other_categories,
    SUM(Quantity) AS Quantity_for_other_categories,  
    Match
  FROM `project.dataset.table`
  WHERE category != 3
  GROUP BY id, Type_1, Match
)
USING(id, Match)
-- ORDER BY id   

If to apply to sample data from your question - output is如果适用于您问题中的示例数据 - output 是

Row id  cat_3   Quantity_3  Other_categories    Quantity_for_other_categories   Match    
1   123 B       6           A                   19                              no match     
2   456 A       4           A                   14                              match    

You can do conditional aggregation.您可以进行条件聚合。 Assuming that type_1 , type_2 and match are fixed for each id , you could do:假设type_1type_2match对于每个id都是固定的,你可以这样做:

select 
    id,
    type_2 cat_3,
    sumif(quantity, category = 3) quantity_3,
    type_1 other_categories,
    sumif(quantity, category <> 3) quantity_for_other_catgories,
    match
from mytable
group by id, type_2, type_1, match

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL CountIf 根据平均值从另一列满足条件 - SQL CountIf a condition is met from another column based on average 如何根据 BigQuery 中另一列的条件显示值的计数 - How to show a count of values based on condition of another column in BigQuery Bigquery:根据另一个表中设置的条件更新列 - Bigquery: Update column based on condition set in another table SQL:根据另一列中是否至少有一个值满足特定条件来创建列 - SQL: Creating a column depending on whether there is at least one value in another column that fulfils a certain condition or not 如何将一个表中的列与 BigQuery 中另一个表中的数组进行比较? - How to compare column in one table with array from another table in BigQuery? 根据 redshift 中另一列的值创建列 - Create column based on values on another column in redshift 如何将一列的数据加载到同一个表的另一列中 - How can I load data of one column into another column in the same table 将 500GB 数据从一个 GCP 存储桶同步到另一个 - syncing 500GB of data from one GCP bucket to another 可以在 BigQuery 中以原子方式将数据从一个表移动到另一个表吗? - Possible to atomically move data from one table to another in BigQuery? Flutter 作为 stream 将数据从一个屏幕传递到另一个屏幕 - Flutter passing data from one screen to another as a stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM