Skip to Main Content
  • Questions
  • Distinct count across multiple tables

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Sean.

Asked: February 12, 2019 - 3:57 pm UTC

Last updated: June 09, 2020 - 4:16 pm UTC

Version: 12C

Viewed 10K+ times! This question is

You Asked

I have three possible places where accounts data can be requested. On the MONTHLY USAGE, HISTORY USAGE or ENROLLMENT tables. The same account(s) could be on all three tables, or on the table(S) multiple times, but some accounts could be on one or two tables but not the third, etc.

For example:

MONTHLYUSAGE    HISTORY    ENROLL

123                        123

456             456       

                456



Is it possible to count the DISTINCT accounts across ALL THREE tables? In this example there would be 2 distinct accounts....






and Chris said...

Unioning the tables together will give you the distinct rows in the unioned columns:

create table t1 (
  c1 int
);

create table t2 (
  c1 int
);

insert into t1 values ( 1 );
insert into t1 values ( 2 );

insert into t2 values ( 2 );
insert into t2 values ( 3 );

select * from t1
union 
select * from t2;

C1   
   1 
   2 
   3 


So all you have to do from here is count the output!

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Using 19c bitmap command for distinct count

BeefStu, June 06, 2020 - 10:14 pm UTC

How can this be written using the bitmap command and called from a function function



     Create table t1 (val1 number);

     INSERT INTO T1 VALUES (1);
     INSERT INTO T1 VALUES (1);
     INSERT INTO T1 VALUES (1);
     INSERT INTO T1 VALUES (1);


     INSERT INTO T1 VALUES (2);
     INSERT INTO T1 VALUES (2);
     INSERT INTO T1 VALUES (2);
     INSERT INTO T1 VALUES (2);


     INSERT INTO T1 VALUES (3);
     INSERT INTO T1 VALUES (3);
     INSERT INTO T1 VALUES (3);
     INSERT INTO T1 VALUES (3);
     INSERT INTO T1 VALUES (3);
     INSERT INTO T1 VALUES (3);

    




Chris Saxon
June 08, 2020 - 10:28 am UTC

What exactly is it you're wanting to do here? And how does it relate to the original question about combining tables?

BeefStu, June 08, 2020 - 8:20 pm UTC

It doesn't relate to the original question about 2 tables but I was looking for an easy to follow example using the bitmap function, which connor talked about in his bitmap distinct count video. Apologies if this isn't related
Chris Saxon
June 09, 2020 - 4:16 pm UTC

As the text when you submit reviews says...

Remember - this is about feedback to the original question. AskTOM is about building a strong Oracle community, not stealing time away from others needing assistance by sneaking in a brand new question under the guise of "Review".

Please submit a new question about this; you can use the "Notify Me" option to get an email when we ope for questions again.

In the meantime, you may find this post useful:

https://danischnider.wordpress.com/2019/04/20/bitmap-based-countdistinct-functions-in-oracle-19c/

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.