2014-12-07 3 views
1

제가 질의 한 두 가지 결과를에 일치하는 것은SQL 테이블

sales      | percent 
30 (this is sum below 100) | 10 
230(this is sum between 100 and 200) | 20 
430(this is sum between 200 and 300) | 30 
630(this is sum between 300 and 400) | 40 
이 같은 smthing 얻을 수있는 방법은

sales 
10 
20 
110 
120 
210 
220 
310 
320 

있다

나는 PHP 배열과 루프로 할 생각이지만 서버에 많은 요청을 보내지 않는 것을 선호한다. 어떤 제안?

+0

배열 및 루프를 사용하면 실제로 추가 요청을 피할 수 있습니다. –

+0

Bonatoc 내가 많은 데이터를 요청할 때 많은 데이터를 보내려고했는데 어디서 닫으려고 할까? – funny

+0

테이블 구조를 명확하게 만들 수 있습니까? –

답변

1

하위 쿼리가 쿼리를 여러 번 실행하고 같은 PHP 으로 그렇게하는 것이 더있을 것입니다 좋은 없다 (대신 쿼리의 난이 $ 제 $ 두 번째 배열)

$first = array(
    array(
     'amount' => 100, 
     'percent' => 10 
    ), 
    array(
     'amount' => 200, 
     'percent' => 20 
    ), 
    array(
     'amount' => 300, 
     'percent' => 30 
    ), 
    array(
     'amount' => 400, 
     'percent' => 40 
    ) 
); 
$second = array(
    array(
     'sales' => 10 
    ), 
    array(
     'sales' => 20 
    ), 
    array(
     'sales' => 110 
    ), 
    array(
     'sales' => 120 
    ), 
    array(
     'sales' => 210 
    ), 
    array(
     'sales' => 220 
    ), 
    array(
     'sales' => 310 
    ), 
    array(
     'sales' => 320 
    ) 
); 

$result = array(); 
$second_len = count($second); 
$s=0; 
for ($i=0,$len=count($first); $i < $len; $i++) { 
    $sum = 0; 
    for (;$s < $second_len; $s++) { 
     if ($second[$s]['sales'] <= $first[$i]['amount']) { 
      $sum += $second[$s]['sales']; 
     } else { 
      break; 
     } 
    } 
    $result[] = array(
     'sales' => $sum, 
     'percent' => $first[$i]['percent'] 
    ); 
} 

var_dump($result); 
1

먼저 상관 된 하위 쿼리를 사용하여 두 번째 결과 집합의 각 행에 대한 정확한 백분율을 찾습니다. 이에 의해 그런 집계 :

select percent, sum(sales) 
from (select q2.*, 
      (select q1.percent 
       from query1 q1 
       where q1.amount >= q2.sales 
       order by q1.amount desc 
       limit 1 
      ) as percent 
     from query2 q2 
    ) q 
group by percent; 
+0

오류 : 알 수없는 열 'q.amount'where where – funny

+0

@funny. . . 그것은'q2.sales' 였을 것입니다. –

1

두 가지 "쿼리 결과"가 있다고 가정 해보십시오. 나는 그들이 테이블에서 온 것 같아요. 그래서, 당신은 다음과 같은 두 개의 테이블이 있다고 가정 :

thresholds 
---------- 
amount | percent 
100 | 10 
200 | 20 
300 | 30 
400 | 40 

sales 
----- 
amount 
10 
20 
110 
120 
210 
220 
310 
320 

이 쿼리 결과를 얻을 수 : SQLFiddle

이 합리적으로 빠른 실행한다 : 당신은 여기에 쿼리를 확인할 수 있습니다

SELECT 
    SUM(sales_amount) sales 
    ,threshold_percent percent 
FROM (
    SELECT 
    s.amount sales_amount 
    ,t.amount threshold_amount 
    ,t.percent threshold_percent 
    FROM sales s 
    INNER JOIN thresholds t 
    ON t.amount > s.amount 
    LEFT JOIN thresholds t2 
    ON t2.amount < t.amount 
    AND t2.amount > s.amount 
    WHERE t2.amount IS NULL 
) sales_percents 
GROUP BY threshold_amount 

인덱스가 thresholds.amount이고 sales.amount 인 경우

+0

사실이 아니라면 6 개의 서로 다른 테이블에서 왔습니다. – funny

+0

@funny 그런 다음 (1) 제안 된 쿼리를 적절한 하위 쿼리로 바꾸거나 (2) 임시 테이블을 사용하여 대체 할 수 있습니다. 물론, 관련된 쿼리가 값이 비싸거나 복잡하면 다른 사람들이 제안한 것처럼 응용 프로그램 코드에서 계산하는 것이 더 나을 수도 있습니다. – abl