如果增量值大于0.25,如果范围与花药分开,我如何从点表中找到可用范围 (我有点表我想生成范围表)
POINTS table
+----+------+------+------------+
| id | sph | cyl | othertbl_id|
+----+------+------+------------+
| 1 | 0 | 0 | 1 |
| 2 | 0 | 0.25 | 1 |
| 3 | 0 | 0.50 | 1 |
| 4 | 0 | 1.00 | 1 |
| 5 | 0.25 | 0 | 1 |
| 6 | 0.25 | 0.25 | 1 |
| 7 | 0.25 | 0.50 | 1 |
| 8 | 0.25 | 1.00 | 1 |
| 9 | 3.25 | 0 | 1 |
| 10 | 3.25 | 0.25 | 1 |
| 11 | 3.50 | 0 | 1 |
| 12 | 3.50 | 0.25 | 1 |
| 13 | 3.75 | 0 | 1 |
| 14 | 3.75 | 0.25 | 1 |
+----+-------------+------------+
我想生成看起来像这样的范围表
+----+----------+----------+----------+----------+------------+
| id | min_sph | max_sph | min_cyl | max_cyl |othertbl_id |
+----+----------+----------+----------+----------+------------+
| 1 | 0 | 0.25 | 0 | 1.00 | 1 |
| 2 | 3.25 | 3.75 | 0 | 0.25 | 1 |
+----+----------+----------+----------+----------+------------+
类似的案例场景使这更容易理解
我之前生成的范围表是我想要的结果。小组说othertbl是产品表,每个产品在每个点的范围都有不同的库存数量,让我们说T恤A可用于年龄0-2和高度从30-50开始,也可用于28-30岁和100-120的高度,这些范围的所有点都是针对相同的T恤排。如果年龄差异超过一年或高度差超过10被认为是新的范围
在这种情况下,点表将如下所示
+----+------+------+------------+
| id | age | hgt | othertbl_id|
+----+------+------+------------+
| 1 | 0 | 30 | 1 |
| 2 | 0 | 40 | 1 |
| 3 | 0 | 50 | 1 |
| 4 | 1 | 30 | 1 |
| 5 | 1 | 40 | 1 |
| 6 | 1 | 50 | 1 |
| 7 | 2 | 30 | 1 |
| 8 | 2 | 40 | 1 |
| 9 | 2 | 50 | 1 |
| 10 | 28 | 100 | 1 |
| 11 | 28 | 110 | 1 |
| 12 | 28 | 120 | 1 |
| 13 | 29 | 100 | 1 |
| 14 | 29 | 110 | 1 |
| 14 | 29 | 120 | 1 |
| 14 | 30 | 100 | 1 |
| 14 | 30 | 110 | 1 |
| 14 | 30 | 120 | 1 |
+----+-------------+------------+
范围表将如下所示
+----+----------+----------+----------+----------+------------+
| id | min_age | max_age | min_hgt | max_hgt |othertbl_id |
+----+----------+----------+----------+----------+------------+
| 1 | 0 | 2 | 30 | 50 | 1 |
| 2 | 28 | 30 | 100 | 120 | 1 |
+----+----------+----------+----------+----------+------------+
答案 0 :(得分:0)
问题不是很清楚,无论如何,这里有一个变体,它会返回非常接近预期结果的“某些东西”:
select min(id) as id, sph as min_sph, (select min(sph) from points as b where b.sph>a.sph) as max_sph, min(cyl) as min_cyl, max(cyl) as max_cyl, othertbl_id as othertbl_id
from points as a
group by othertable_id, sph
结果是:
id minsph maxsph mincyl maxcyl othertable_id
1 0 0.25 0 1 1
5 0.25 3.25 0 1 1
9 3.25 3.5 0 0.25 1
11 3.5 3.75 0 0.25 1
13 3.75 (null) 0 0.25 1