2010-07-24 143 views
1

由於某種原因,我無法繞過此查詢,因此我想知道是否有人能夠在此幫助我。我有以下3個表:MySQL查詢分組子查詢問題

opp_cstm:

 
id_c make_c  time_followup_c lead_category_c lead_type_c 
9  GMC   224    GM    Internet Sales 
e  Buick  809    GM Internet  Service 
8  GMC   1559   Dealer Web  Sales 
2  Cadillac  10596   Dealer Web  Service 
3  Chevrolet 15595   GM Internet  Sales 
4  Chevrolet 905   GM Internet  Service 

機會:

 
id date_entered   deleted 
2 2010-07-16 16:46:21  0 
3 2010-07-16 16:55:53  0 
4 2010-07-16 19:30:12  0 
8 2010-07-16 16:44:13  0 
9 2010-07-16 16:39:17  0 
e 2010-07-16 16:41:44  0 

leads_objectives:

 
makes_carried resp_time_obj 
GMC   18000 
Ford    7200 
Cobalt   43200 
Chevrolet  18000 
Buick   18000 
Cadillac   7200 

我需要得到如下的佈局(這顯然會按日期,LCAT分組):

日期:

 
Date LCat   LType  #ofLds AvgResp  #LdsRespOT %LdsRespOT #Lds!RespOT %Lds!RespOT 
19-Jul GM Internet Sales  10  18 minutes  7   70%   3   30% 
19-Jul GM Internet Service 20  20 minutes  10  50%   10   50% 
19-Jul Handraiser Sales  10  45 minutes  5   50%   5   50% 
20-Jul Dealer Web Sales  20  120 minutes 5   25%   15   75% 
20-Jul Dealer Web Service 10  7 minutes 3   30%   7   70% 

每一列,我需要說明 opportunities.date_entered =今日(此必須對課程的一切)

轉移酶: opp_cstm.lead_category

LTYPE:運p_cstm.lead_type

#ofLds:這就需要將其中刪除=機會伯爵 「0」 而導致分類不爲空

AvgResp:魅力。在機會timefollowup-C字段,其中已刪除=「0」和鉛類別不是null,和time_followup_c> 0並且不爲空的

#LdsRespOT: Count的機會,其中已刪除=「0」,並導致分類是不NULL和time_followup_c小於或等於resp_time_obj AND make_c = makes_carried和time_followup_c> 0並且不爲空

%LdsRespOT:(#LdsRespOT/#ofLds)

#Lds RespOT:(# ofLds - #LdsRespOT)

!210

%數據Lds RespOT:(!#Lds RespOT/#ofLds)

我有一個很難讓我的頭圍繞此查詢。我想知道這裏有人能否提供某種協助?我將如何正確寫這個查詢?

我已經嘗試了幾次,但每次都失敗,我感到沮喪!我知道我只是缺少一些某種類型的分組或某種我缺少的子查詢。

任何幫助將不勝感激!

謝謝!

回答

0

任何人誰碰到這個,可能需要這樣的幫助來了,這裏是我落得這樣做:

SELECT 
opportunities.date_entered as Date, 
opportunities_cstm.lead_category_c as LCat, 
opportunities_cstm.lead_type_c as LType, 
count(opportunities.id) as '# of Lds', 
SUM(opportunities_cstm.time_followup_c)/count(opportunities.id) as AvgResp, 
SUM(
    CASE 
    WHEN ( 
     opportunities_cstm.time_followup_c IS NOT NULL 
     AND 
     opportunities_cstm.time_followup_c < leads_handling_objectives.resp_time_obj 
     ) 
    THEN 
     1 
    ELSE 
     0 
    END 
    ) as '#LdsRespOT', 
(SUM(
    CASE 
    WHEN ( 
     opportunities_cstm.time_followup_c IS NOT NULL 
     AND 
     opportunities_cstm.time_followup_c < leads_handling_objectives.resp_time_obj 
     ) 
    THEN 
     1 
    ELSE 
     0 
    END 
    ) /count(opportunities.id))*100 as '%LdsRespOT', 
count(opportunities.id) - SUM(
    CASE 
    WHEN ( 
     opportunities_cstm.time_followup_c IS NOT NULL 
     AND 
     opportunities_cstm.time_followup_c < leads_handling_objectives.resp_time_obj 
     ) 
    THEN 
     1 
    ELSE 
     0 
    END 
    )as '#Lds!RespOT', 
((count(opportunities.id) - SUM(
    CASE 
    WHEN ( 
     opportunities_cstm.time_followup_c IS NOT NULL 
     AND 
     opportunities_cstm.time_followup_c < leads_handling_objectives.resp_time_obj 
     ) 
    THEN 
     1 
    ELSE 
     0 
    END 
    ))/count(opportunities.id))*100 as '%Lds!RespOT' 
FROM 
opportunities 
INNER JOIN 
opportunities_cstm 
ON 
    opportunities_cstm.id_c = opportunities.id 
AND 
    opportunities_cstm.lead_category_c IS NOT NULL 
AND 
    opportunities_cstm.lead_category_c NOT LIKE '' 
INNER JOIN 
leads_handling_objectives 
ON 
    leads_handling_objectives.makes_carried = opportunities_cstm.make_c 
WHERE 
opportunities.date_entered = DATE(NOW()) 
AND 
opportunities.deleted='0' 
GROUP BY 
opportunities_cstm.lead_category_c