Link to home
Start Free TrialLog in
Avatar of Steve A
Steve AFlag for United States of America

asked on

How do I get a count in a query being used?

Oracle 11g.  No PL/SQL used for this query.

How do I get a count of how many rows retrieved from the result set
and have it displayed as a column within the query?
Say that I run the query example below, if there was 140 columns and
a "big" where clause and it retrieved 20 rows.  I want to display the count
within the column - RowCount below.
Thanks.

Select column1,
           RowCount,
           column2,
           column3,
           .
           .
           .
          column140
From MyTable1,
          AnotherTable,
          AndAnother
Where Clause Statements..
..
..
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve A

ASKER

That did it!  THANK YOU.
Avatar of Steve A

ASKER

Question, now would that be a total count if I had a couple of union statements?
Meaning I need a total count of all the records retrieved, not individual select statements.
Thanks.

Select
  Count(*) over() As RowCount
  .

UNION
Select
 .
 .
UNION
Select
 .
 .
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Wrap all the unions in an inline view, then do the count outside the view:

select dummy, count(*) over() from
(
     select dummy from dual
     union
     select dummy from dual
     union
     select dummy from dual
);
Avatar of Steve A

ASKER

Sorry, getting this error ORA-00904: "DUMMY": invalid identifier, when running this:

Select dummy, Count(*) Over() As RowCount From
(
SELECT
    (select dummy from dual),
    Col1,
    .
    Col140
UNION
SELECT
    (select dummy from dual),
    Col1,
    .
    Col140
UNION
SELECT
    (select dummy from dual),
    Col1,
    .
    Col140
);
dummy is a column in the DUAL table.

That was just an example of how you do it.

You need to move your entire unioned select statement inside of parans, then select every column and the COUNT in the outside select.

For example, you have:
create table tab1(col1 char(1), col2 char(1), col3 char(1));
create table tab2(col4 char(1), col5 char(1), col6 char(1));

The it goes:

select mycol1, mycol2, mycol3, count(*) over() from
(
select col1 mycol1 col2, mycol2, col3 mycol3 from tab1
union
select col4, col5, col5 from tab2
);


In a union the first select names the columns for the inline view.  As you can see, just for grins, I aliased the columns with 'my'.
Avatar of Steve A

ASKER

Whew!  That'll be a lot of columns on the outside!  140 of them  :-)

select col1, col2, col3........col140, count(*) over() from
(
SELECT --Outpatients...
    Col1,
    .
    Col140
UNION
SELECT --Inpatients...
    Col1,
    .
    Col140
SELECT  --Alternate Care...
    Col1,
    .
    Col140
);
>>That'll be a lot of columns on the outside!  140 of them  :-)

Let me save you some time...  alias the inline view then you can select *.

I also corrected several typos I had in the above example (I edited that post as well).

Here's a complete test case.

drop table tab1 purge;
drop table tab2 purge;

create table tab1(col1 char(1), col2 char(1), col3 char(1));
create table tab2(col4 char(1), col5 char(1), col6 char(1));

insert into tab1 values('a','b','c');
insert into tab1 values('d','b','f');

insert into tab2 values('d','b','f');
commit;


select t.*, count(*) over() from
(
select col1 mycol1, col2 mycol2, col3 mycol3 from tab1
union
select col4, col5, col6 from tab2
) t;

Open in new window

Avatar of Steve A

ASKER

Sorry to keep bugging you on a fine Friday afternoon before the weekend!
It seems that it doesn't like the 't.*'...

ORA-00918: column ambiguously defined

select t.*, count(*) over() from
(
SELECT --Outpatients...
    Col1,
    .
    Col140
UNION
SELECT --Inpatients...
    Col1,
    .
    Col140
UNION    
SELECT  --Alternate Care...
    Col1,
    .
    Col140
) t;
>>Sorry to keep bugging you

No problem!

>>on a fine Friday afternoon

It's almost 8PM where I am....  beer opened a while ago!   ;)

I've got all night...

>>ORA-00918: column ambiguously defined

I assume you tested the massive union query all by itself?

What is your Oracle version (all 4 numbers please, like 10.2.0.4).

I ran what I posted against 11.2.0.3.

Can you try my test case as-posted against a dev/test database on the same version you are running?
Avatar of Steve A

ASKER

I added all 140 columns on the top and works like a charm.  Thanks again!
No problem.

I was thinking about adding all the columns...  Should not have even been that much typing (sorry if it was).

Copy/paste the first query in the union and you have all the columns with all the names.