[英]SQL Redshift: How to use a value in a join based on another columns value
我有一个包含如下列的表:
name product existing_customer
john crisps yes
paul cheese no
george broccoli no
ringo spam yes
但为了填充此表,我引用了此布局:
name value. tag
john crisps. product
john yes. existing_customer
george broccoli. product
george no. existing_customer
etc etc
我需要像第一个表一样的 output,我已经用 case 语句尝试过它,但它显示了具有相同名称的多行并分别显示了每个值。
有没有办法在 redshift SQL 中做到这一点?
您可以使用聚合:
select name,
max(case when tag = 'product' then value end) as product,
max(case when tag = 'existing_customer' then value end) as existing_customer
from t
group by name;
使用新的输入,类似于:
select * from
( select name, value as product
from in_values
where tag='product') as p
full outer join
( select name, value as existing
from in_values
where tag='existing_customer') as e
using (name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.