当我的WHERE子句将其过滤掉时,如何在我的数据集中包含结果?

时间:2017-10-14 20:51:44

标签: mysql sql filter google-bigquery mode-analytics

我想列出所有销售人员的名单,并计算他们在上周完成了多少次演示。

我正在使用模式分析,因此{{@dummy_accounts}}和{{@active_sdrs}}只是子查询的快捷方式。第一个只是获取我们不想计算的帐户ID列表,第二个是当前正在工作的SDR。

挑战在于我的过滤器带走了上周安排了0个演示的任何SDR。我仍然希望将它们包含在我的结果集中,以表明他们没有安排任何演示。

如何获得那些没有安排演示的特别提款权?

SELECT full_name,
       count(activity_id) AS total_demos
FROM by_task
WHERE task_type LIKE 'Demo'
  AND created_date > DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK)
  AND account_id NOT IN
    (SELECT account_id
     FROM {{@dummy_accounts}})
  AND full_name IN
    (SELECT full_name
     FROM {{@active_sdrs}})
GROUP BY 1
ORDER BY 2 DESC

- 编辑 -

这就是我希望数据集看起来像......

full_name   total_demos
John Doe    5
Billy Bob   3
Ray Mac     2
Jose McGuerro   0
Joe MacDonald   0

但是,这是查询产生的内容......

full_name   total_demos
John Doe    5
Billy Bob   3
Ray Mac     2

这里有几行显示数据结构...

activity_id task    date    full_name   account_id
324123ASe1Q Demo    2017-10-13  John Doe    aa912
324123ASe3F Demo    2017-10-13  John Doe    aa932
324123ASe8E Demo    2017-10-09  Billy Bob   aa933
324123ASe9A Demo    2017-10-08  Ray Mac aa999
324123ASe9A Demo    2017-10-09  Ray Mac aa993

挑战在于没有演示的人不会出现在此表中。

也许我可以做一个工会,只是给他们一个Demo的任务,而不是给他们任何activity_ids所以他们不算数?不确定。

2 个答案:

答案 0 :(得分:2)

您似乎需要left join。我猜:

SELECT a.full_name, count(t.activity_id) AS total_demos
FROM {{@active_sdrs}} a left join
     by_task t
     on t.full_name = a.full_name and
        t.task_type LIKE 'Demo'
        t.created_date > DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK) and
        t.account_id NOT IN (SELECT da.account_id FROM {{@dummy_accounts}} da)
GROUP BY 1
ORDER BY 2 DESC;

NOT IN相当可疑。它通常写成:

SELECT a.full_name, count(t.activity_id) AS total_demos
FROM {{@active_sdrs}} a left join
     by_task t
     ON t.full_name = a.full_name and
        t.task_type LIKE 'Demo' and
        t.created_date > DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK) LEFT JOIN
     {{@dummy_accounts}} da
     ON t.account_id = da.account_id
WHERE da.account_id IS NULL
GROUP BY 1
ORDER BY 2 DESC

答案 1 :(得分:2)

以下是BigQuery Standard SQL

   
#standardSQL
WITH `by_task` AS (
  SELECT '324123ASe1Q' activity_id, 'Demo' task_type, DATE '2017-10-13' created_date , 'John Doe' full_name, 'aa912' account_id UNION ALL
  SELECT '324123ASe3F', 'Demo', DATE '2017-10-13', 'John Doe', 'aa932' UNION ALL
  SELECT '324123ASe8E', 'Demo', DATE '2017-10-09', 'Billy Bob', 'aa933' UNION ALL
  SELECT '324123ASe9A', 'Demo', DATE '2017-10-08', 'Ray Mac', 'aa999' UNION ALL
  SELECT '324123ASe9A', 'Demo', DATE '2017-10-09', 'Ray Mac', 'aa993' 
),
dummy_accounts AS (
  SELECT '1111' account_id UNION ALL 
  SELECT '2222' UNION ALL 
  SELECT '3333'
),
active_sdrs AS (
  SELECT 'John Doe' full_name UNION ALL
  SELECT 'Billy Bob' UNION ALL
  SELECT 'Ray Mac' UNION ALL
  SELECT 'Jose McGuerro' UNION ALL
  SELECT 'Joe MacDonald'
)
SELECT a.full_name, IFNULL(total_demos, 0) total_demos
FROM active_sdrs a 
LEFT JOIN (
  SELECT full_name, COUNT(activity_id) AS total_demos
  FROM by_task
  WHERE task_type LIKE 'Demo'
  AND created_date > DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK)
  AND account_id NOT IN (SELECT account_id FROM dummy_accounts)
  AND full_name IN (SELECT full_name FROM active_sdrs)
  GROUP BY 1
) t
ON t.full_name = a.full_name 
ORDER BY 2 DESC

正如预期的那样 - 输出

full_name        total_demos     
-------------    -----------
John Doe                   2     
Ray Mac                    2     
Billy Bob                  1     
Jose McGuerro              0     
Joe MacDonald              0