Search code examples
sqlimpala

Sum the count(*) of between three tables


I want to sum the number of counts between 3 tables. I have added three input fields to give a specific date each time but I am struggling on how to SUM the COUNTS(*)

select count(*)
from db.table1
where call_date = ${var:call_date};

select count(*)
from db.table2
where call_date = ${var:call_date};

select count(*)
from  db.table3
where call_date= ${var:call_date};

thanks in advance


Solution

  • UNION ALL the selects. SUM() the result.

    select sum(cnt) from
    (
        select count(*) cnt
        from db.table1
        where call_date = ${var:call_date}
        UNION ALL
        select count(*)
        from db.table2
        where call_date = ${var:call_date}
        UNION ALL
        select count(*)
        from  db.table3
        where call_date= ${var:call_date}
    ) dt