2010-07-24 227 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