天道酬勤,学无止境

sum

MySQL SUM multiple columns

I have another question about sum. I'd like to sum the scored of a baseball team, adding the scored when playing as local with the score when playing as visitor. The matches table is like this: Baseball_matches (Id, IdTeamHome, IdTeamAway, ScoreHome, ScoreAway, Status) I'd like to group by Team, showing the total score for each team by adding the scored of matches that have the status os "played" For each team: SUM ScoreHome when IdTeamHome + SUM ScoreAway when IdTeamAway How can I do this? Thanks.

2021-06-24 08:25:05    分类:问答    mysql   sum

python function to find sum of positive numbers in an array

i need to write a function that takes an array of numbers and finds the maximum sum of all the numbers. In other words, I need to find the sum of just the positive numbers. I wrote this, I'm getting "list is out of range" thoughts? def maximum_sub(A): x = 0 i = 0 for i in A: while A[i] > 0: x+=A[i] i+=1 return x

2021-06-24 06:55:09    分类:问答    python   arrays   sum

Java 8 Stream 将元素添加到列表和求和(Java 8 Stream add elements to list and sum)

问题 我相信我可以在 listOfPricedObjects 上使用一个流操作来做下一步: List<BigDecimal> myList = new ArrayList(); myList = listOfPricedObjects.stream().map(PricedObject::getPrice).collect(Collectors.toList()); BigDecimal sum = listOfPricedObjects.stream().map(PricedObject::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add) 我如何用价格填充 myList 并一次使用流计算价格总和? 谢谢 UPD:结果我需要 myList 填充价格和 sum 变量和 sum。 但不是使用 stream() 两次。 回答1 您可以在应用缩减时使用peek并添加到新list List<BigDecimal> newList = new ArrayList<>(); BigDecimal sum = list.stream() .map(PricedObject::getPrice) .peek(newList::add) .reduce(BigDecimal.ZERO, BigDecimal::add)

2021-06-24 06:53:34    分类:技术分享    java   collections   java-8   sum   java-stream

为什么 2 个 parseFloat 变量的总和给我一个不正确的十进制数 [重复](Why does the sum of 2 parseFloat variables give me an incorrect decimal number [duplicate])

问题 这个问题在这里已经有了答案: 浮点数学被破坏了吗? (30 个回答) 6年前关闭。 如果我有这个小功能: <script type="text/javascript"> function printFloat(){ var myFloatNumber1 = document.getElementById('floatNumber1'); var myFloatNumber2 = document.getElementById('floatNumber2'); alert(parseFloat(myFloatNumber1.value) + parseFloat(myFloatNumber2.value)) } </script> <input type="text" id="floatNumber1"></input> <input type="text" id="floatNumber2"></input> <input type="button" onclick="printFloat()"/> 在字段 1 我输入:221.58 在字段 2 我输入:2497.74 我希望输入字段中 2 个数字的总和为 2 个数字:2719.32 但结果是一个不正确的数字...:2719.3199999999997 一轮可以完成这项工作,但我只是不明白为什么代码会在这个数字上这样做

2021-06-24 02:04:44    分类:技术分享    javascript   function   sum

如何在 Python 3.5 中找到给定范围内的素数之和?(How do I find the sum of prime numbers in a given range in Python 3.5?)

问题 我设法使用以下方法创建了给定范围内的素数列表: import numpy as np num = int(input("Enter a number: ")) for a in range(2,num+1): maxInt=int(np.sqrt(a)) + 1 for i in range(2,maxInt): if (a%i==0): break else: print (a) 我现在想找到范围内所有素数的总和,所以我只是放下 print (sum(a)) 但是当尝试这样做时,我得到以下回溯: Traceback (most recent call last): File "C:/Users/Jason/PycharmProjects/stackidiots/scipuy.py", line 11, in <module> print(sum(a)) TypeError: 'int' object is not iterable 回答1 在您的情况下, a是循环中使用的整数变量,而不是可迭代的。 import numpy as np num = int(input("Enter a number: ")) primes = [] for a in range(2,num+1): maxInt= int(np.sqrt(a)) + 1 for i in range(2

2021-06-23 23:50:57    分类:技术分享    python   sum   primes

算法将数组拆分为子数组,其中所有子数组中的最大和尽可能低(Algorithm splitting array into sub arrays where the maximum sum among all sub arrays is as low as possible)

问题 假设我们有一个整数数组:a = {2,4,3,5} 我们有 k = 3。 我们可以将数组 a 拆分为 k (3) 个子数组,其中数组的顺序不能改变。 每个子阵列的总和必须尽可能低,以便所有子阵列之间的最大总和尽可能低。 对于上述解决方案,这将给出 {2, 4}, {3}, {5},其最大和为 6 (4 + 2)。 错误的答案是 {2}, {4, 3}, {5},因为在这种情况下最大和是 7 (4 + 3)。 我尝试创建一个贪心算法,该算法通过将所有整数相加并将其除以子数组的结果数量来计算整个数组的平均值。 所以在上面的例子中,这意味着 14 / 3 = 4(整数除法)。 然后它会将数字加到计数器中,只要它小于平均数字即可。 然后它将重新计算子数组的其余部分。 我的解决方案给出了一个很好的近似值,可以用作启发式方法,但并不总是给我正确的答案。 有人可以帮我解决一个算法,该算法为我提供所有情况下的最佳解决方案并且比 O(N²) 更好吗? 我正在寻找一种大约为 O(n log n) 的算法。 提前致谢! 回答1 我们可以使用二分查找来解决这个问题。 所以,假设所有子数组的最大值是x ,那么,我们可以在 O(n) 中贪婪地选择每个子数组,使得每个子数组的和最大并且小于或等于x 。 创建完所有子数组后,如果子数组的个数小于或等于k ,则x是一种可能的解决方案,否则,我们增加x 。

2021-06-23 21:56:32    分类:技术分享    arrays   algorithm   split   sum   min

C# LINQ : Dynamically create property and values that includes sum of a value from a grouped query

My Title is slightly confusing so I will explain it here. This is my sample query and what I wish to achieve: var results = from row in toSearch.AsEnumerable() group by row.Field<String>("Somename") into grp select new { GroupMe = grp.Key, foreach (var k in someDic.Keys){ TODOPROPERTYNAMEFROMk = grp.sum(x => grp.k); } }; This is the general idea of what I want to achieve. Based on a dictionary of keys, I want to create property names from it while summing up the data from the grouped query. The sum will be based off the value of the keys. toSearch is a datatable and all of the keys of the

2021-06-23 17:43:07    分类:问答    c#   arrays   linq   select   sum

SUM values in SQL starting from a specific point in another table

I have a table that lists the index/order, the name, and the value. For example, it looks like this: TABLE1: ID | NAME | VALUE 1 | A | 2 2 | B | 5 3 | C | 2 4 | D | 7 5 | E | 0 Now, I have another table that has a random list of NAMEs. It'll just show either A, B, C, D, or E. Depending on what the NAME is, I wanted to calculate the SUM of all the values that it will take to get to E. Does that make sense? So if for example, my table looks like this: TABLE2: NAME D B A I'd want another column next to NAME that'll show the sum. So D would have 7 because the next event is E. B would have to be

2021-06-23 16:57:33    分类:问答    sql   sql-server   database   sum   nested-queries

从 2 个不同的表中计算(数量*价格)的总和(Calculating the SUM of (Quantity*Price) from 2 different tables)

问题 我有两个表如下 PRODUCT表 Id | Name | Price 和一个ORDERITEM表 Id | OrderId | ProductId | Quantity 我想要做的是,计算每个产品的小计价格(数量*价格),然后对整个订单的总价值求和。 我正在尝试这样的事情 SELECT Id, SUM(Quantity * (select Price from Product where Id = Id)) as qty FROM OrderItem o WHERE OrderId = @OrderId 但这当然行不通:) 任何帮助表示赞赏! 编辑:我只想显示整个订单的总计,所以基本上是 OrderItem 中每一行的 Quantity*Price 的总和。 这是一些示例数据。 样本数据 餐桌产品 Id Name Price 1 Tomatoes 20.09 4 Cucumbers 27.72 5 Oranges 21.13 6 Lemons 20.05 7 Apples 12.05 表订单项 Id OrderId ProductId Quantity 151 883 1 22 152 883 4 11 153 883 5 8 154 883 6 62 米 回答1 用: SELECT oi.orderid, SUM(oi.quantity * p.price) AS

2021-06-23 13:29:13    分类:技术分享    sql   sum

此 SP 中“SUM”附近的“语法不正确”在哪里?(Where is the 'Incorrect syntax near 'SUM'' in this SP?)

问题 当我尝试在 Visual Studio 中执行此 MS SQL Server 存储过程时(从服务器资源管理器中创建它后) ,我得到“消息 102,级别 15,状态 1,过程duckbilledPlatypi,'SUM' 附近的第 21 行语法错误): CREATE PROCEDURE [dbo].[duckbilledPlatypi] @Unit varchar(25), @BegDate datetime, @EndDate datetime AS DECLARE @Week1End datetime, @Week2begin datetime Select Description, @BegDate BegDate, @Week1End Week1End, @Week1End Week2Begin, @EndDate EndDate, SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Usage ELSE 0 END) Week1Usage, SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.Usage ELSE 0 END) Week2Usage, SUM(CASE WHEN Ind

2021-06-23 11:03:18    分类:技术分享    sql-server   stored-procedures   sum