比较两个不同的SQL表的计数

时间:2018-10-04 23:29:48

标签: sql presto

我有两个表A和B,并试图像

那样比较它们的计数
<h1 class="clipped-border">
  A heading with a solid red border
</h1>

将其作为中间步骤时出现错误。 我的目的是使输出为

SELECT COUNT(*) FROM table_a WHERE day = '2018-09-27' AS A,
COUNT(*) FROM table_b WHERE day = '2018-09-27' AS B;

2 个答案:

答案 0 :(得分:1)

简单的方法

select 
    A,
    B,
    A-B as difference
From
(
select
(SELECT COUNT(*) FROM table_a WHERE day = '2018-09-27') as A,
(select COUNT(*) FROM table_b WHERE day = '2018-09-27') as B
) Res

答案 1 :(得分:1)

尝试一下:

SELECT COALESCE(a.day, b.day) AS day
     , count_a
     , count_b
     , count_a - count_b AS difference
  FROM ( SELECT day, count(*) AS count_a FROM table_a GROUP BY day ) a
  FULL JOIN ( SELECT day, count(*) AS count_b FROM table_b GROUP BY day ) b
       ON b.day = a.day