通过比较一组列值创建一个新列 [英] Create a new column by comparing set of column values
本文介绍了通过比较一组列值创建一个新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何根据表中的product_id"对一组行进行分组,并通过比较组内的一组列值来创建新列?
How can I group a set rows by 'product_id' from a table and create a new column by comparing a set of column values within the group ?
例如
product id part_id availability
1 1 true
1 2 false
1 3 true
2 1 true
2 4 true
2 8 true
通过比较可用性",我想创建一个可行性"列.如果缺少单个零件,则不可行.
By comparing the 'availability',I would like to create a 'feasibility' column. If single part is missing it would't be feasible.
product_id feasibility
1 false
2 true
我使用 GROUP BY 子句按 product_id 对它们进行排序,但我无法弄清楚第二部分.
I used GROUP BY clause to sort'em out by product_id but I can't figure out the second part.
推荐答案
如果您只对问题中显示的两列感兴趣,有一个非常简单的解决方案:
There is a very simple solution available, if you are only interested in the two columns shown in your question:
select product_id, min(availability) feasible
from tbl group by product_id
请看这里的小演示:.http://rextester.com/GMT80394
这篇关于通过比较一组列值创建一个新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文