首页 > 解决方案 > 根据sql中的字段值乘以字段

问题描述

我有这种表:

name,code1,code2
a,3,
b,5,
c,10,
a,,10
b,,30
c,,50

编辑

我的表是这样的,我将始终在字段中有两行具有相同值name

我的桌子

我想在name字段上乘以具有相同值的行,所以得到这样的东西:

name,final
a,30
b,150
c,500

我也在考虑将以前的表格转换为这个表格: 在此处输入图像描述

标签: sql

解决方案


如果每个名称总是有两行,其中一个(仅)具有 code1 的值,而另一行(仅)具有 code2 的值,则可以使用自联接

select t1.name, t1.code1, t2.code2, t1.code1 * t2.code2
from the_table t1 
  join the_table t2 on t1.name = t2.name and t2.code1 is null
where t1.code2 is null;

推荐阅读