我有用于计算总和的sql:
SELECT l.name, CASE WHEN l.child_leads_ids <> '' THEN SUM(kosten_einkauf) + (SELECT SUM(cl.kosten_einkauf) FROM leads as cl WHERE cl.lead_id IN (l.child_leads_ids)) ELSE SUM(l.kosten_einkauf) END AS sumValue FROM leads as l WHERE (l.name LIKE '%Test%' || l.first_name LIKE '%Test%' || l.nick_name LIKE '%Test%' || l.name_vorname_partner LIKE '%Test%') AND l.is_active = 1 AND l.is_archive = 0 AND dont_show_in_list=0 and l.is_parent_lead <> 2 GROUP BY l.name
我如何添加仅适用于cl.is_active = 1 AND cl.is_archive = 0 AND cl.dont_show_in_list=0
重复查询,格式化。
SELECT l.name,
CASE WHEN l.child_leads_ids <> ''
THEN SUM(kosten_einkauf) + (
SELECT SUM(cl.kosten_einkauf)
FROM leads as cl
WHERE cl.lead_id IN (l.child_leads_ids))
ELSE SUM(l.kosten_einkauf) END AS sumValue
FROM leads as l
WHERE (l.name LIKE '%Test%' ||
l.first_name LIKE '%Test%' ||
l.nick_name LIKE '%Test%' ||
l.name_vorname_partner LIKE '%Test%')
AND l.is_active = 1 AND l.is_archive = 0
AND dont_show_in_list=0
and l.is_parent_lead <> 2
GROUP BY l.name