Ich weiß nicht einmal, was ich technisch fragen, so war es hart zu versuchen, eine Antwort zu finden. Ich habe diese Abfrage in MySQL arbeiten, aber Wechsel zu PostgreSQL war ein wenig schwierig.mehrere Fall Ergebnisse mit einer Gruppe von
SELECT call.call_id,
SUM(CASE WHEN indicator.indicator_name = 'Appointment Set' THEN indicator_score.score_value ELSE '0' END) as "Appointment Set" ,
SUM(CASE WHEN indicator.indicator_name = 'Ask for Business' THEN indicator_score.score_value ELSE '0' END) as "Ask for Business"
FROM call
JOIN indicator_score ON indicator_score.call_id = call.call_id
JOIN indicator ON indicator.indicator_id = indicator_score.indicator_id
JOIN org_unit ON org_unit.org_unit_id = call.org_unit_id
WHERE call.org_unit_id IN (4147, 4153)
group by call.call_id, indicator_name
Jeder call_id
kann und sollte einen Score für beide „für Business-Fragen“ und „Termin vereinbaren“, aber was ich bekomme ist ein mit der Note und der andere mit einem ‚0‘.
call_id | Appointment Set | Ask for Business
--------------------------------------------
3,001,144 | 0 | 89
3,001,145 | 100 | 0
Was ich suche ist so etwas wie dieses:
call_id | Appointment Set | Ask for Business
--------------------------------------------
3,001,144 | 100 | 89
ich auch diese Struktur versucht haben, aber ich bin die gleichen Ergebnisse zu erzielen.
SELECT call.call_id,
CASE indicator.indicator_name WHEN 'Appointment Set' THEN sum(indicator_score.score_value) ELSE '0' END as Appointment_Set ,
CASE indicator.indicator_name WHEN 'Ask for Business' THEN sum(indicator_score.score_value) ELSE '0' END as Ask_for_Business
FROM call
JOIN indicator_score ON indicator_score.call_id = call.call_id
JOIN indicator ON indicator.indicator_id = indicator_score.indicator_id
JOIN org_unit ON org_unit.org_unit_id = call.org_unit_id
WHERE call.org_unit_id IN (4147, 4153)
group by call.call_id, indicator_name
Das Problem ist, dass die 'call_id' unterschiedlich ist, so dass Sie zwei Ergebniszeilen erhalten. Wenn es das gleiche wäre, würden Sie nur eine Ergebniszeile erhalten. Ich denke also, dass die Abfrage so funktioniert, wie Sie es erwarten. –