2010-07-24 6 views
1

J'ai de la difficulté à comprendre cette question pour une raison quelconque et je me demandais si quelqu'un serait capable de m'aider ici. Je les 3 tableaux suivants:MySQL Query GroupingSubQuery Question

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 

occasions:

 
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 

Je suis besoin pour obtenir la mise en page suivante (ce sera évidemment regroupées par date, 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% 

Explication de chaque colonne I besoin:

Date: opportunities.date_entered = aujourd'hui (ce doit être sur tout bien sûr)

LCAT: opp_cstm.lead_category

LTYPE: op p_cstm.lead_type

#ofLds: cela doit être le comte de possibilités où deleted = "0" et la catégorie Le plomb est non nul

AvgResp: Moy. de champ timefollowup-C dans les occasions où Deleted = « 0 » et la catégorie Le plomb est non nulle et et time_followup_c> 0 et non null

#LdsRespOT: Nombre d'occasions où Deleted = « 0 » et la catégorie de plomb n'est pas nULLE eT time_followup_c est inférieur ou égal à resp_time_obj eT make_c = makes_carried et time_followup_c> 0 et non null

% LdsRespOT: (#LdsRespOT/#ofLds)

#Lds RespOT: (# ofLds - #LdsRespOT)

! 210

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

J'ai du mal à obtenir ma tête autour de cette requête. Je me demandais si quelqu'un d'ici pouvait nous aider d'une façon ou d'une autre? Comment est-ce que j'écrirais cette question correctement?

J'ai essayé plusieurs fois mais j'échoue à chaque fois et je suis frustré! Je sais qu'il me manque juste un groupement quelconque ou une sorte de sous-requête sql qui me manque.

Toute aide serait grandement appréciée!

Merci!

Répondre

0

Pour toute personne qui vient à travers ce qui pourrait avoir besoin d'aide avec quelque chose comme ça, voici ce que je fini par faire:

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