SQL仅在特定情况下运行脚本的一部分

时间:2019-05-01 13:35:32

标签: mysql sql

我有用于计算总和的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

0 个答案:

没有答案