简体   繁体   English

如何找到等于给定总和的所有组合?

[英]How to find all combinations that equal a given sum?

In SQL I have a list of transaction types and a value. 在SQL中,我有一个事务类型列表和一个值。 Simple example below. 简单的例子如下。

|-----------|--------|
| Tran.Type | Value  | 
|-----------|--------|
|     04    |   10   |
|-----------|--------|
|     07    |   10   |
|-----------|--------|
|     04    |   5    |
|-----------|--------|
|     01    |   8    | 
|-----------|--------|



|-----------------|
| Current Balance |
|-----------------|
|        13       |
|-----------------|

Goal is to sum values and get number 13. In this example is easy to find that type 07 is negative. 目标是对值求和并获得数字13.在此示例中很容易发现类型07为负数。 So 10-10+5+8=13 所以10-10 + 5 + 8 = 13

But when number of transaction gets to 20 & 10 different types is quite difficult. 但是当交易数量达到20和10种不同类型时非常困难。 Also values are positive & negative. 值也是正面和负面的。

So far I came up with this. 到目前为止,我想出了这个。

import itertools
numbers = list(map(int, input("Enter multiple values: ").split()))
expectedSum = int(input("Enter expected value: "))
result = [seq for i in range(len(numbers), 0, -1) for seq in 
itertools.combinations(numbers, i) if sum(seq) == expectedSum]
print (result)<code>    

Any idea how can I incorporate type to result section? 任何想法如何将类型合并到结果部分? and if that type is an opposite value or not? 如果该类型是相反的值?

There are a couple of ways to do this, by using CASE in your SQL statement OR if you have a VERY large list then introduce a field which will contain +1 or -1 based on the Transaction Type, then when you are performing the values MULTIPLY the value and finally do a sum. 有几种方法可以做到这一点,在SQL语句中使用CASE ,或者如果你有一个非常大的列表然后根据事务类型引入一个包含+1或-1的字段,那么当你执行这些值时MULTIPLY值,最后做一笔总和。 Example, is here; 例子,在这里;

ALTER TABLE Tran ADD COLUMN tran_multiple INT default 1;

-- update each value as req.
UPDATE Tran set tran_multiple=-1 WHERE Tran.id = xxxx; 

SELECT sum (TranValue.Value * Tran.tran_multiple) as Val From TranValue 
    INNER JOIN Tran on TranValue.Type = Tran.Type 

Here I am assuming that your table TranValue stores the values. 在这里,我假设您的表TranValue存储值。 Of course, you can use Group By clause GROUP BY Tran.type in your statement, along with Tran.type in your SELECT field list (for listing by Tran.Type & Values). 当然,你可以使用Group By子句GROUP BY Tran.type在你的声明,随着Tran.type在SELECT字段列表(由Tran.Type和价值观上市)。 Then writing a simple code should not be a problem. 然后编写一个简单的代码应该不是问题。

Though, I would request you to refrain from using VALUE or VALUES in your SQL as it is very similar to the RESERVED KEYWORDs ( just a suggestion ) 虽然,我会请求你不要在你的SQL中使用VALUEVALUES ,因为它与RESERVED KEYWORDs非常相似( 只是一个建议

I am not putting the CASE example here since you didn't specify which Database you are using (syntax varies) and also since you claim that there are a large number of types. 我没有在这里放置CASE示例,因为您没有指定您正在使用的数据库(语法各不相同),并且因为您声称存在大量类型。

If you prefer coding instead of SQL, then the same can be done by defining a dictionary where each type holds +1 or -1 depending on the type and then when the user inputs the values you can multiply type and get it done. 如果你喜欢的编码,而不是SQL,则同样可以通过定义字典,其中每种类型的成立做了+1或-1取决于类型 ,然后当用户输入的值,你可以乘类型和完成它。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找具有给定总和的数字列表的所有组合 - Find all combinations of a list of numbers with a given sum 如何找到它们的总和等于给定数字的数字? - How to find the numbers that their sum equal to a given number? 如何找到总和最多为一个常数的所有组合? - How to find all combinations that sum up to at most a constant? 如何查找列表中所有产品的不同组合的总和(python) - How to find sum of different combinations of all products in list (python) 如何找到所有组合 - How to find all combinations 在n个数组中找到一个等于给定值的总和? - Find a sum in n arrays equal to a given value? 在 pandas 数据框中查找所有可能的组合并求和 - To find all possible combinations and sum in pandas dataframe 查找 0、1 的所有组合,它们的总和为 1 或 2,长度/大小不同 - Find all combinations of 0, 1 that sum to 1 or 2 at varying lengths / sizes 如果应该存在一个公共元素,如何找到列表中给出的所有可能组合? - How to find all possible combinations given in a list, given there should exist a common element? 给定一个数字列表,如何创建总和的所有组合并返回这些总和的列表 - Given a list of numbers, how do you create all the combinations of sums and return a list of those sum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM