2013-04-26 5 views
0

다음은 작동중인 쿼리입니다. 모든 하위 쿼리를 직접 공동 쿼리로 대체하고 싶습니다. 친절하게 해결책을 제안하십시오.하위 쿼리를 공동 쿼리로 대체하는 방법은 무엇입니까?

SELECT 
    id, 
    startTime, 
    endTime, 
    drawingTime, 
    result, 
    wpAmount, 
    lpAmount, 
    prize, 
    cntWinTickets, 
    cntLoosTickets 
FROM (SELECT 
    i.id   AS id, 
    i.start_time AS startTime, 
    i.end_time  AS endTime, 
    i.drawing_time AS drawingTime, 
    i.result  AS result, 
    SUM(t.amount) AS wpAmount, 
    SUM(t.prize) AS prize, 
    COUNT(t.id) AS cntWinTickets 
     FROM issues i 
    LEFT JOIN orders o 
     ON o.issue_id = i.id 
    LEFT JOIN tickets t 
     ON t.order_id = o.id 
     AND t.has_prize = 1 
     GROUP BY i.id) AS A 
    INNER JOIN (SELECT 
     i.id   AS lid, 
     SUM(t.amount) AS lpAmount, 
     COUNT(t.id) AS cntLoosTickets 
      FROM issues i 
     LEFT JOIN orders o 
      ON o.issue_id = i.id 
     LEFT JOIN tickets t 
      ON (t.order_id = o.id 
       AND (t.has_prize = 0 
       OR t.has_prize IS NULL)) 
      GROUP BY i.id) AS B 
    ON A.id = B.lid 
+0

아마도 ** VIEW ** –

+0

을 작성하면 스키마와 원하는 출력을 게시 할 수 있습니다. 몇 가지 샘플 데이터와 함께 해결하는 것이 더 편할 것입니다 –

답변

2

다음은 SUM 연산자에 멋진 교차 제품이 포함 된 솔루션입니다. 나는 이것을하지 않는 것이 좋을 것이라고 생각하지만 그것은 재미 있습니다!

SELECT i.id AS id, 
     i.start_time AS startTime, 
     i.end_time AS endTime, 
     i.drawing_time AS drawingTime, 
     i.result AS result, 
     SUM(t.amount*COALESCE(t.has_prize,0)) AS wpAmount, 
     SUM(t.prize*COALESCE(t.has_prize,0)) AS prize, 
     COUNT(case when t.has_prize=1 then t.id end) AS cntWinTickets, 
     SUM(t.amount*(1-COALESCE(t.has_prize,0))) AS lpAmount, 
     COUNT(case when COALESCE(t.has_prize,0)=0 then t.id end) AS cntLoosTickets 
FROM issues i 
LEFT JOIN orders o ON o.issue_id = i.id 
LEFT JOIN tickets t ON t.order_id = o.id AND t.has_prize in (null,0,1) 
GROUP BY i.id 
+0

이것은 무엇입니까? 왜 여기에't.amount * COALESCE (t.has_prize, 0)' –

+0

't.has_prize'가'1','0' 또는'NULL'입니까? 't.has_prize'가'1' 일 때 더하기를 원한다면, 교차 제품을 할 수 있습니다. 그리고 반대는't.amount * (1-COALESCE (t.has_prize, 0))'에 적용됩니다. –

1

시도 :

여기
SELECT i.id AS id, 
     i.start_time AS startTime, 
     i.end_time AS endTime, 
     i.drawing_time AS drawingTime, 
     i.result, 
     SUM(case when t.has_prize=1 then t.amount end) AS wpAmount, 
     SUM(case when t.has_prize=1 then t.prize end) AS prize, 
     COUNT(case when t.has_prize=1 then t.id end) AS cntWinTickets, 
     SUM(case when coalesce(t.has_prize,0)=0 then t.amount end) AS lpAmount, 
     COUNT(case when coalesce(t.has_prize,0)=0 then t.id end) AS cntLoosTickets 
FROM issues i 
LEFT JOIN orders o ON o.issue_id = i.id 
LEFT JOIN tickets t ON t.order_id = o.id AND t.has_prize in (null,0,1) 
GROUP BY i.id 
1

당신이 0t.amount 다른 걸릴 t.has_prize = 1 이상의 경우는

SELECT 
    i.id   AS id, 
    i.start_time AS startTime, 
    i.end_time  AS endTime, 
    i.drawing_time AS drawingTime, 
    i.result  AS result, 
    SUM(IF t.has_prize = 1,t.amount,0) AS wpAmount, 
    SUM(IF t.has_prize = 1,t.prize,0) AS prize, 
    COUNT(IF t.has_prize = 1,t.id,0) AS cntWinTickets, 
    SUM(IF t.has_prize = 0,t.amount,0) AS lpAmount, 
    COUNT(IF t.has_prize = 0,1,0) AS cntLoosTickets 
FROM issues i 
    LEFT JOIN orders o ON o.issue_id = i.id 
    LEFT JOIN tickets t ON t.order_id = o.id 
GROUP BY i.id 

IF t.has_prize = 1,t.amount,0 뜻 결합 할 수있는 방법입니다. Mark가 사용한 CASE가 번갈아 나타납니다.

관련 문제