Solved

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

Posted on 2013-10-25
12
519 Views
Last Modified: 2013-10-25
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..
..
..
0
Comment
Question by:Machinegunner
  • 6
  • 6
12 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 39601563
Try this:

Select column1,
           count(*) over() RowCount,
...
0
 

Author Comment

by:Machinegunner
ID: 39601619
That did it!  THANK YOU.
0
 

Author Comment

by:Machinegunner
ID: 39601634
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
 .
 .
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39601717
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
);
0
 

Author Comment

by:Machinegunner
ID: 39601872
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
);
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39601889
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'.
0
 

Author Comment

by:Machinegunner
ID: 39601905
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
);
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39601916
>>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

0
 

Author Comment

by:Machinegunner
ID: 39601938
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;
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39601964
>>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?
0
 

Author Comment

by:Machinegunner
ID: 39601965
I added all 140 columns on the top and works like a charm.  Thanks again!
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39601971
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
passing parameters to sql script oracle 4 61
SQL query question 8 92
ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword 2 63
date show only hh:mm 2 27
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question