Sql-某些行的总和是否在A和B之间?
我目前正试图找到,如果一些行的总和是A和B之间的内容 例如,我需要一个介于100 m²和105 m²之间的曲面, 因此,请求应添加所有行,直到总和在100 m²和105 m²之间,然后尝试所有可能的解决方案 我所拥有的 我想要达到的结果 50+55=105 id_biens 001返回trueSql-某些行的总和是否在A和B之间?,sql,sum,rows,cube,rollup,Sql,Sum,Rows,Cube,Rollup,我目前正试图找到,如果一些行的总和是A和B之间的内容 例如,我需要一个介于100 m²和105 m²之间的曲面, 因此,请求应添加所有行,直到总和在100 m²和105 m²之间,然后尝试所有可能的解决方案 我所拥有的 我想要达到的结果 50+55=105 id_biens 001返回true ________________________________ | id | id_biens | surface | |____|_____________|____________
________________________________
| id | id_biens | surface |
|____|_____________|____________|
| 2 | 001 | 50 |
| 4 | 001 | 55 |
|____|_____________|____________|
谢谢你阅读我的帖子 一些数据。我添加了一行,以说明重复的曲面区域
CREATE TABLE Table1
(id int, id_biens char(3), surface int)
;
INSERT INTO Table1 VALUES
(1, '001', 80),
(2, '001', 50),
(3, '001', 30),
(4, '001', 55),
(5, '001', 50);
这对双人来说很有用。很难将其推广到SQL中任意数量的行。这本质上是一个组合问题。您可能需要检查每一个可能的行组合,因此在最坏的情况下,您需要生成并计算行集合{1,2,3,4,12,13,…123,124…1234},例如使用此处的id号
对于1000行,一次取四行可以得到大约410亿个组合。统计软件可能是解决这类问题的最佳选择
我认为,一般来说,下面的查询是一个更好的方法。但记住我说过的统计软件。它显著地改变了您的输出,但我认为这种改变是为了更好。更清楚的是,哪些行构成了整个曲面
select distinct b1,
id_1, id_2,
s1, s2,
(s1 + s2) total_surface
from
(select t1.id id_1, t1.id_biens b1, t1.surface s1,
t2.id id_2, t2.id_biens b2, t2.surface s2
from Table1 t1
inner join Table1 t2
on t1.id_biens = t2.id_biens
and t1.id <> t2.id
where t1.id < t2.id
) required_alias
where s1 + s2 between 100 and 105
order by b1, id_1, id_2;
b1 id_1 id_2 s1 s2 total_surface
--
001 2 4 50 55 105
001 2 5 50 50 100
001 4 5 55 50 105
三个值的组合。您需要进行的更改位于注释中
select distinct b1,
id_1, id_2, id_3, -- Add an id,
s1, s2, s3, -- Add a surface
(s1 + s2 + s3) total_surface -- Add surface to the total.
from
(select t1.id id_1, t1.id_biens b1, t1.surface s1,
t2.id id_2, t2.id_biens b2, t2.surface s2,
t3.id id_3, t3.id_biens b3, t3.surface s3 -- Third of three sets of columns.
from Table1 t1
inner join Table1 t2
on t1.id_biens = t2.id_biens
and t1.id <> t2.id
inner join Table1 t3 -- Additional join.
on t1.id_biens = t3.id_biens
and t1.id <> t3.id
where t1.id < t2.id
and t2.id < t3.id -- Additional restriction
) required_alias
where s1 + s2 + s3 between 100 and 105 -- Correct math here, too.
求和的行是否需要具有相同的id值?是的,它需要具有相同的id值,2000行大约有500个id值…我重写了我的查询以连接id值,并简化了它。它工作得非常好,除了做2个曲面的和,但它也应该尝试做4个曲面的和,5.它必须尝试所有组合。有时对于相同的id,我有15个曲面。我知道尝试所有组合都需要时间,但这是必须的。多谢各位much@TheBigJohn:嗯,我的观点是,似乎需要不同的SQL查询来计算每个组合数。很难将其推广到SQL中任意数量的行。我认为您最好将数据存储在SQL数据库中,并使用应用程序代码来执行求和的穷举枚举或动态编程。好的,Mike,我想使用不同的SQL来计算每个数字组合,但是我是SQL新手,在我知道如何选择好的行之后,我不知道如何为表中的所有组合生成所有和。谢谢lot@TheBigJohn:更新答案。
select distinct b1,
id_1, id_2, id_3, -- Add an id,
s1, s2, s3, -- Add a surface
(s1 + s2 + s3) total_surface -- Add surface to the total.
from
(select t1.id id_1, t1.id_biens b1, t1.surface s1,
t2.id id_2, t2.id_biens b2, t2.surface s2,
t3.id id_3, t3.id_biens b3, t3.surface s3 -- Third of three sets of columns.
from Table1 t1
inner join Table1 t2
on t1.id_biens = t2.id_biens
and t1.id <> t2.id
inner join Table1 t3 -- Additional join.
on t1.id_biens = t3.id_biens
and t1.id <> t3.id
where t1.id < t2.id
and t2.id < t3.id -- Additional restriction
) required_alias
where s1 + s2 + s3 between 100 and 105 -- Correct math here, too.