簡體   English   中英

MySql查詢和GROUP BY,我需要做些什么不同的事情?

[英]MySql query and GROUP BY, what do I need to do differently?

現在我有一個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`

問題是,在指定LEFT JOIN之后,您正在過濾WHERE子句中的客戶,因此沒有銷售的銷售員被排除在外,因為這些記錄的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`

也許你可以把你所擁有的東西與沒有銷售的銷售代表聯系起來,並且只需將它們硬編碼為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`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM