0

现在我有一个 sql 查询,它选择我的销售代表范围,然后加入一个客户表,并计算某些值是否为真。但是,我的查询有效,它忽略了未售出任何客户的销售代表。我希望此查询返回所有销售代表,即使他们没有创建任何符合条件的客户记录。我需要改变什么?

这是我的 SQL 查询的链接:http: //pastie.org/4557540(下同)

SELECT u.id                                                           AS
       `employee_id`,
       u.`first_name`                                                 AS
       `employee_first_name`,
       u.`last_name`                                                  AS
       `employee_last_name`,
       (SELECT Count(*)
        FROM   saleset s
        WHERE  s.pitchedby_id = `employee_id`
               AND s.pitchstartedat BETWEEN '2012-08-20 00:00:00' AND
                                            '2012-08-20 23:59:59')    AS
       `transfers_taken`,
       Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL))                AS
       `total_closes`,
       Count(IF(c.`saletype_id` = 1, 1, NULL))                        AS
       `regular_sale`,
       Count(IF(c.`saletype_id` = 2, 1, NULL))                        AS
       `postdated_sale`,
       Count(IF(c.`saletype_id` = 4, 1, NULL))                        AS
       `attempted_sale`,
       Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL))     AS
       `cancel_status`,
       Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS
       `pending_completion_status`,
       Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL))      AS
       `complete_status`,
       Count(IF(c.`customerstatus_id` = 20, 1, NULL))                 AS
       `postdate_pending`,
       Count(IF(c.`customerstatus_id` = 25, 1, NULL))                 AS
       `postdate_declined`
FROM   `user` u
       LEFT JOIN customer c
              ON c.`salesrep_id` = u.id
WHERE  u.id IN ( 39, 65, 76, 96,
                 195, 266, 349, 401,
                 402, 404, 405, 407,
                 411, 412 )
       AND c.`activationdate` BETWEEN
           '2012-08-20 00:00:00' AND '2012-08-20 23:59:59'
GROUP  BY u.`id`
4

2 回答 2

2

问题是您正在过滤WHERE子句中的客户,在LEFT JOIN指定之后,所以没有销售的推销员被排除在外,因为c.activationdatenull这些记录。

解决方案是让客户端在连接条件中进行过滤:

(...)
FROM   `user` u
       LEFT JOIN customer c
              ON (c.`salesrep_id` = u.id and
                  u.id IN ( 39, 65, 76, 96, 195, 266, 349, 401,
                            402, 404, 405, 407,
                            411, 412 ) 
                  AND c.`activationdate` BETWEEN
                      '2012-08-20 00:00:00' AND '2012-08-20 23:59:59')

GROUP  BY u.`id`
于 2012-08-20T17:50:02.623 回答
0

也许您可以将您与没有销售的销售代表联合起来,然后将它们硬编码为 0。例如:

SELECT u.id                                                           AS 
       `employee_id`, 
       u.`first_name`                                                 AS 
       `employee_first_name`, 
       u.`last_name`                                                  AS 
       `employee_last_name`, 
       (SELECT Count(*) 
        FROM   saleset s 
        WHERE  s.pitchedby_id = `employee_id` 
               AND s.pitchstartedat BETWEEN '2012-08-20 00:00:00' AND 
                                            '2012-08-20 23:59:59')    AS 
       `transfers_taken`, 
       Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL))                AS 
       `total_closes`, 
       Count(IF(c.`saletype_id` = 1, 1, NULL))                        AS 
       `regular_sale`, 
       Count(IF(c.`saletype_id` = 2, 1, NULL))                        AS 
       `postdated_sale`, 
       Count(IF(c.`saletype_id` = 4, 1, NULL))                        AS 
       `attempted_sale`, 
       Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL))     AS 
       `cancel_status`, 
       Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS 
       `pending_completion_status`, 
       Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL))      AS 
       `complete_status`, 
       Count(IF(c.`customerstatus_id` = 20, 1, NULL))                 AS 
       `postdate_pending`, 
       Count(IF(c.`customerstatus_id` = 25, 1, NULL))                 AS 
       `postdate_declined` 
FROM   `user` u 
       INNER JOIN customer c 
              ON c.`salesrep_id` = u.id 
WHERE  u.id IN ( 39, 65, 76, 96, 
                 195, 266, 349, 401, 
                 402, 404, 405, 407, 
                 411, 412 ) 
       AND c.`activationdate` BETWEEN 
           '2012-08-20 00:00:00' AND '2012-08-20 23:59:59' 
GROUP  BY u.`id`
UNION
SELECT u.id                                                           AS 
       `employee_id`, 
       u.`first_name`                                                 AS 
       `employee_first_name`, 
       u.`last_name`                                                  AS 
       `employee_last_name`, 
       0                                                              AS 
       `transfers_taken`, 
       Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL))                AS 
       `total_closes`, 
       Count(IF(c.`saletype_id` = 1, 1, NULL))                        AS 
       `regular_sale`, 
       Count(IF(c.`saletype_id` = 2, 1, NULL))                        AS 
       `postdated_sale`, 
       Count(IF(c.`saletype_id` = 4, 1, NULL))                        AS 
       `attempted_sale`, 
       Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL))     AS 
       `cancel_status`, 
       Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS 
       `pending_completion_status`, 
       Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL))      AS 
       `complete_status`, 
       Count(IF(c.`customerstatus_id` = 20, 1, NULL))                 AS 
       `postdate_pending`, 
       Count(IF(c.`customerstatus_id` = 25, 1, NULL))                 AS 
       `postdate_declined` 
FROM   `user` u 
       LEFT JOIN customer c 
              ON c.`salesrep_id` = u.id 
WHERE  u.id IN ( 39, 65, 76, 96, 
                 195, 266, 349, 401, 
                 402, 404, 405, 407, 
                 411, 412 ) 
       AND c.`activationdate` BETWEEN 
           '2012-08-20 00:00:00' AND '2012-08-20 23:59:59'
       AND c.`salesrep_id` IS NULL 
GROUP  BY u.`id`
于 2012-08-20T17:54:41.837 回答