8

I have a list of tables for example:

mytableA
mytableB
mytableC

The tables all have same column (timestamp).

I can do a count on each table individually:

select count(*) from mytableA where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000';
select count(*) from mytableB where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000';
select count(*) from mytableC where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000';

How can I combine this in one single query? Is there an easy way?

Expected Results:

MyTableName     MyCnt
-----------     -----
mytableA        121
mytableB        78
mytableC        2345

7 Answers 7

11
SELECT  (
    SELECT COUNT(*)
    FROM   table1
    ) AS tot1,
    (
    SELECT COUNT(*)
    FROM   table2
    ) AS tottab2,
    (
    SELECT COUNT(*)
    FROM   table3
    ) AS tottab3
Sign up to request clarification or add additional context in comments.

1 Comment

that simply elegant solution beats it all IMO
4

You can't do it directly with a query like where table in (myTableA, myTableB, etc) But you can pretify the union all solution:

select MyTableName, count(*)
FROM(
  select 'myTableA' MyTableName, timestamp from mytableA 
  union all
  select 'myTableB', timestamp  from mytableB  
  union all
  select 'myTableA', timestamp  from mytableC  
)
WHERE timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
GROUP BY MyTableName;

1 Comment

thanks. this is much prettier. I did have to add GROUP BY MyTableName to get it to work. Thanks.
3

I am not sure about Oracle, in SQLserver you can do this as,

select (select count(*) from table1) + (select count(*) from table2)

Update: or like this,

select (select count(*) from table1) ,(select count(*) from table2)

OR,

(select count(*) from table1) union (select count(*) from table2)

Comments

1
select 'myTableA' MyTableName, count(*) MyCnt from mytableA where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
union all
select 'myTableB', count(*) from mytableB where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
union all
select 'myTableC', count(*) from mytableC where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000';

Comments

1

Using Oracle 11gR2:

select     
  (select count(*) from mytableA where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000') tabA,
  (select count(*) from mytableB where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000') tabB,
  (select count(*) from mytableC where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000') tabC
from dual;

Results in:

tabA| tabB| tabC
----|-----|-----
 121|   78| 2345

Comments

0

Try this

select 'mytableA' as tablename, count(*) from mytableA where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
union all
select 'mytableB' as tablename , count(*) from mytableB where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
union all
select'mytableB' as tablename , count(*) from mytableC where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'

Comments

-3

How about this?

SELECT 
     a, b, c 
FROM 
     (select count(*) from mytableA where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000') AS a, 
     (select count(*) from mytableB where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000') AS b, 
     (select count(*) from mytableC where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000') AS c

dunno whether this works or not though :/

1 Comment

Please do not answer blindly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.