当我的 WHERE 子句过滤掉结果时,如何将结果包含在我的数据集中?
How do I include results in my dataset when my WHERE clause filters them out?
我想列出所有销售人员并计算他们在上周完成了多少次演示。
我正在使用模式分析,所以 {{@dummy_accounts}} 和 {{@active_sdrs}} 只是子查询的快捷方式。第一个只是获取我们不想计算的帐户 ID 列表,第二个是当前正在工作的 SDR。
挑战在于我的过滤器会删除上周安排了 0 次演示的所有 SDR。而且我仍然想将它们包含在我的结果集中,以表明他们没有安排任何演示。
我怎样才能得到它,以便包括那些没有安排演示的 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
挑战在于没有演示的人不会出现在这个 table 中。
也许我可以做一个工会,只给他们一个演示任务,而不给他们任何 activity_ids 这样他们就不算数了?没有把握。
看来您需要 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
以下适用于 BigQuery 标准 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
我想列出所有销售人员并计算他们在上周完成了多少次演示。
我正在使用模式分析,所以 {{@dummy_accounts}} 和 {{@active_sdrs}} 只是子查询的快捷方式。第一个只是获取我们不想计算的帐户 ID 列表,第二个是当前正在工作的 SDR。
挑战在于我的过滤器会删除上周安排了 0 次演示的所有 SDR。而且我仍然想将它们包含在我的结果集中,以表明他们没有安排任何演示。
我怎样才能得到它,以便包括那些没有安排演示的 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
挑战在于没有演示的人不会出现在这个 table 中。
也许我可以做一个工会,只给他们一个演示任务,而不给他们任何 activity_ids 这样他们就不算数了?没有把握。
看来您需要 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
以下适用于 BigQuery 标准 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