Sql 根据其他列中的数据修改列的内容
我得到的数据如下所示-Sql 根据其他列中的数据修改列的内容,sql,group-by,google-bigquery,Sql,Group By,Google Bigquery,我得到的数据如下所示- category_id category Type_1 Type_2 no_of_items 123 cat_A A both 5 123 cat_A B both 10 123 cat_B B both 35 123 cat_B A
category_id category Type_1 Type_2 no_of_items
123 cat_A A both 5
123 cat_A B both 10
123 cat_B B both 35
123 cat_B A both 10
123 cat_C A both 20
我想达到以下结果-
当category_id时,category是相同的,Type_2是'both'(Type_2中有许多其他类型)-
检查类型_1是否有a和B的记录,如果有,则将类型_1更改为“两者”,否则保持原样,并将项目的数量相加
如果类型_1有a或B记录,则保持类型_1不变
结果应该是这样的-
category_id category Type_1 Type_2 no_of_items
123 cat_A both both 15
123 cat_B both both 45
123 cat_C A both 20
如果我理解正确,那么下面的查询将通过执行
求和(无项)
来工作。这是postgres中的解决方案,但同样的解决方案应该适用于BigQuery
select
category_id,
category,
case when total = 2 then 'both' else Type_1 end as Type_1,
Type_2,
sum(no_of_items) as no_of_items
from
(select
category_id,
category,
Type_1,
Type_2,
no_of_items,
count(Type_1) over (partition by category) as total
from category
) t
group by
category_id,
category,
case when total = 2 then 'both' else Type_1 end,
Type_2
order by
category
输出:
----------------------------------------------------
category_id category type_1 type_2 no_of_items
----------------------------------------------------
123 cat_A both both 15
123 cat_B both both 45
如果我理解正确,那么下面的查询将通过执行
求和(无项)
来工作。这是postgres中的解决方案,但同样的解决方案应该适用于BigQuery
select
category_id,
category,
case when total = 2 then 'both' else Type_1 end as Type_1,
Type_2,
sum(no_of_items) as no_of_items
from
(select
category_id,
category,
Type_1,
Type_2,
no_of_items,
count(Type_1) over (partition by category) as total
from category
) t
group by
category_id,
category,
case when total = 2 then 'both' else Type_1 end,
Type_2
order by
category
输出:
----------------------------------------------------
category_id category type_1 type_2 no_of_items
----------------------------------------------------
123 cat_A both both 15
123 cat_B both both 45
只需使用聚合:
select category_id, category,
(case when min(Type_1) <> max(Type_2) then 'Both' else min(type_1) end) as type_1,
Type_2,
sum(no_of_items) as no_of_items
from t
where type_2 = 'both'
group by category_id, category, Type_2;
选择类别\u id,类别,
(当最小值(类型_1)最大值(类型_2)然后“两者”时,否则最小值(类型_1)结束)为类型_1,
类型2,
总和(项目的数量)作为项目的数量
从t
式中,类型_2=‘两者’
按类别\u id、类别、类型\u 2分组;
只需使用聚合:
select category_id, category,
(case when min(Type_1) <> max(Type_2) then 'Both' else min(type_1) end) as type_1,
Type_2,
sum(no_of_items) as no_of_items
from t
where type_2 = 'both'
group by category_id, category, Type_2;
选择类别\u id,类别,
(当最小值(类型_1)最大值(类型_2)然后“两者”时,否则最小值(类型_1)结束)为类型_1,
类型2,
总和(项目的数量)作为项目的数量
从t
式中,类型_2=‘两者’
按类别\u id、类别、类型\u 2分组;
请添加更多示例数据,这将有助于更好地解释聚合逻辑。您的数据仅显示发生聚合的情况。“您需要提供一些数据,以显示在不满足聚合条件时的预期结果。”Nick更新了问题。如果不满足聚合条件,即A和B都不存在,则A或B都存在,在这种情况下,我希望保持类型_1不变且不聚合。请添加更多示例数据,这将有助于更好地解释聚合逻辑。您的数据仅显示聚合发生的情况。“您需要提供一些数据,以显示在不满足聚合条件时的预期结果。”Nick更新了问题。如果不满足聚合条件,即A和B都不存在,则A或B都存在,在这种情况下,我希望保持类型_1不变且不聚合。如果仅存在一个Type_1
值,则给出错误答案:@Nick已更新我的答案。假设它没有重复项,只有两个类别A
和B
。您也应该更新演示链接。但就我个人而言,我认为要正确回答这个问题需要太多的假设。在OP更新这个问题之前,几乎所有的事情都是猜测…@热心你的假设是正确的。类型_1只有两个类别A和B。此外,没有重复项。谢谢@用户11035754。子查询是不必要的。这比必要的要复杂得多,需要更多的处理。当只有一个type_1
值存在时,会给出错误的答案:@Nick已经更新了我的答案。假设它没有重复项,只有两个类别A
和B
。您也应该更新演示链接。但就我个人而言,我认为要正确回答这个问题需要太多的假设。在OP更新这个问题之前,几乎所有的事情都是猜测…@热心你的假设是正确的。类型_1只有两个类别A和B。此外,没有重复项。谢谢@用户11035754。子查询是不必要的。这比必要的要复杂得多,需要更多的处理。