Google扩散的表格等于SQL总和(如果(条件,Val1,Val2))



google电子表格等于以下SQL查询?

SELECT SUM (IF (a = 'P', 1, IF (a = 'H', 0.5, 0))) FROM sheet;

我想找到一个派生列的总和,其中派生字段的值为1(当a = 'P')或0.5(当a = 'H'时)。

Update - 基于Kiel Labuca的评论,我获得了一个工作解决方案。这更多的是周围的工作。它之所以起作用,是因为派生值是常数,而不是基于原始值。有真正的解决方案吗?

=COUNTIF(B2:B32, "=P") + COUNTIF(B2:B32, "=H") / 2

Google电子表格和函数概述

添加行或数字列是所有电子表格程序中最常见的操作之一。

为了使完成此任务更容易,Google电子表格包括一个内置的公式,称为sum函数。总和函数的语法

a函数的语法是指函数的布局,并包括函数的名称,括号和参数。

总和函数的语法为:

=SUM(number_1,number_2,...number_30)

最多可以通过该函数求和。总和函数的参数

number_1,number_2,... number_30-要添加的数据

参数可以包含:

a list of numbers to be summed
a list of cell references indicating the location of the data in the worksheet
a range of cell references to the location of the data

示例:使用总和函数添加数字列

Enter the following data into cells A1 to A6: 114, 165, 178, 143, 130, 165
Click on cell A7 - the location where the results of the function will be displayed
Click on Insert > Functions > SUM in the menus insert the SUM function into cell A7
Drag select cells A1 to A6 in the spreadsheet to enter this range as the function's argument
Press the ENTER key on the keyboard
The number " 895 " should appear in the cell A7, this is the sum of the numbers entered in cells A1 to A6
When you click on cell A7 the complete function =SUM ( A1:A6 ) appears in the formula bar above the worksheet

资料来源:Google电子表格sum函数

我不确定我是否明白您的Countif解决方案是解决方法,而不是"不是真正的解决方案",因为从数学上讲,它确实可以完成您需要的工作。您可以以不同的方式表达它:

=ARRAYFORMULA(SUM(COUNTIF(B2:B32,{"P";"H"})*{1;0.5}))

或类似的东西与您的SQL查询示例更明显相似:

=ARRAYFORMULA(SUM(IF(B2:B32="P",1,IF(B2:B32="H",0.5.0))))

,但您的解决方案可能比其中任何一个都更有效。

最新更新