I have a situation where I am joining between two tables, and I need a value from one table, which will be used as the LIMIT coefficient for a subquery in the join. Suppose I have the following [extremely simplified] tables -
data:
experiment_id | value
--------------|--------
1 | 2.5
1 | 2.6
1 | 4.5
1 | 2.3
1 | 3.5
1 | 2.8
2 | 2.3
2 | 1.2
2 | 1.1
2 | 3.6
2 | 3.8
2 | 4.1
2 | 7.9
2 | 4.2
2 | 1.0
data_clip:
experiment_id | clip_index
--------------|------------
1 | 3
2 | 5
I need to sum each experiment the sorted values up to a specific index_ clip, which varies between experiments. So, my result table would ideally look like this:
results:
experiment_id | sum
--------------|-------
1 | 7.6 # => 2.3 + 2.5 + 2.8
2 | 13.0 # => 1.0 + 1.1 + 1.2 + 2.3 + 3.6 + 3.8
, (ruby, python ..), db. , - SQL ( , , , , ):
SELECT
T0.experiment_id as `id`,
(SELECT SUM(x.value) from
(SELECT value
FROM data
WHERE experiment_id = t0.experiment_id
ORDER BY value
LIMIT t0.clip_index ) as x) AS `sum`
FROM data_clip AS t0
:
- LIMIT (1000, 10 ..), .
WHERE t0, .
, , SQL. group_concat substring_index clip_index , ("1.2,2.3,3.2") group_concat (, ~ 100 ). ? .