SQL BigQuery - 比较两个表的总计数

Posted

技术标签:

【中文标题】SQL BigQuery - 比较两个表的总计数【英文标题】:SQL BigQuery - Compare total counts from two tables 【发布时间】:2020-07-10 14:43:14 【问题描述】:

我有两个表,我想比较每个表的事件计数,然后得出差异。

我想检查给定日期并查看表 A 中的 EventType 是否与表 B 中的关联列匹配,例如表 A EventType Bounce count 6 和 Table B total_bounces 166,那么如果我能得到结果表示表 B 等中没有匹配项或类似 +160 的内容

我不确定如何将行与列表进行比较,非常感谢您的建议。

表 A

表 B

【问题讨论】:

【参考方案1】:

以下是 BigQuery 标准 SQL

#standardSQL
SELECT EventDate,
  b.total_sents - a.total_sents AS total_sents_diff,
  b.total_bounces - a.total_bounces AS total_bounces_diff,
  b.total_opens - a.total_opens AS total_opens_diff,
  b.total_clicks - a.total_clicks AS total_clicks_diff
FROM (
  SELECT EventDate,
    SUM(IF(EventType IN ('Bounce', 'Open'), EventCount, 0)) AS total_sents,
    SUM(IF(EventType = 'Bounce', EventCount, 0)) AS total_bounces,
    SUM(IF(EventType = 'Open', EventCount, 0)) AS total_opens,
    SUM(IF(EventType = 'Click', EventCount, 0)) AS total_clicks
  FROM `project.dataset.tableA`
  GROUP BY EventDate
) a
LEFT JOIN `project.dataset.tableB` b
USING(EventDate)

您可以使用您问题中的示例数据进行测试,如以下示例所示

#standardSQL
WITH `project.dataset.tableA` AS (
  SELECT '2020-07-05' EventDate, 'Bounce' EventType, 6 EventCount UNION ALL
  SELECT '2020-07-05', 'Click', 16737 UNION ALL
  SELECT '2020-07-05', 'Open', 187400 UNION ALL
  SELECT '2020-07-06', 'Bounce', 16 UNION ALL
  SELECT '2020-07-06', 'Click', 26737 UNION ALL
  SELECT '2020-07-06', 'Open', 387400 
), `project.dataset.tableB` AS (           
  SELECT '2020-07-05' EventDate, 155057 total_sents, 166 total_bounces, 75361 total_opens, 8783 total_clicks UNION ALL
  SELECT '2020-07-06', 255057, 266, 85361, 9783 
)
SELECT EventDate,
  b.total_sents - a.total_sents AS total_sents_diff,
  b.total_bounces - a.total_bounces AS total_bounces_diff,
  b.total_opens - a.total_opens AS total_opens_diff,
  b.total_clicks - a.total_clicks AS total_clicks_diff
FROM (
  SELECT EventDate,
    SUM(IF(EventType IN ('Bounce', 'Open'), EventCount, 0)) AS total_sents,
    SUM(IF(EventType = 'Bounce', EventCount, 0)) AS total_bounces,
    SUM(IF(EventType = 'Open', EventCount, 0)) AS total_opens,
    SUM(IF(EventType = 'Click', EventCount, 0)) AS total_clicks
  FROM `project.dataset.tableA`
  GROUP BY EventDate
) a
LEFT JOIN `project.dataset.tableB` b
USING(EventDate)    

有输出

Row EventDate   total_sents_diff    total_bounces_diff  total_opens_diff    total_clicks_diff    
1   2020-07-05  -32349              160                 -112039             -7954    
2   2020-07-06  -132359             250                 -302039             -16954   

【讨论】:

以上是关于SQL BigQuery - 比较两个表的总计数的主要内容,如果未能解决你的问题,请参考以下文章

sql数据库中的两个表的记录进行比较!并将结果写入表中!

在 BigQuery 中,您将如何使用两个日期列计算每月和每日总计?

BigQuery SQL如何在使用LIMIT时获取总数

用于组合来自两个表的数据的 SQL 查询

如何比较标准 SQL(BigQuery)中的两个数组?

BigQuery如何获得每月的总数?