[英]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并将类别 3的Type_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_1
、 type_2
和match
对于每个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.