0

現在、営業担当者の範囲を選択し、顧客テーブルを結合し、特定の値が true であるかどうかをカウントする 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

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 に答える