Re: SQL to count rows in Goup By query?

From: Michael Sallwasser <msallwas_at_world.nad.northrop.com>
Date: Thu, 9 Jun 1994 16:21:12 GMT
Message-ID: <Cr51FD.1Av_at_gremlin.nrtc.northrop.com>


In article <1994May30.235803.6724_at_zed.com> bachn_at_diamond.zed.com (Bach Nguyen) writes:
>Hi
>
>Is it possible to write a single sql to get the number of rows returned
>in a Select query containing a Group By clause?
>
>ex: workorder
> =========
>
> workorderID priority
> 1 M
> 2 L
> 3 L
> 4 H
> 5 M
> 6 L
>
>
>sql1) SELECT priority, count(priority) from workorder GROUP BY priority;
>
>res1) priority count(priority)
> M 2
> L 3
> H 1
>
>
> total rows return: 3 rows.
>

If I understood your question correctly, the answer is

        select count(distinct PRIORITY) from WORKORDER;

If you had two columns in your group by clause, the answer would be

       select count(distinct PRIORITY||col2) from WORKORDER;

Note that you are concatenating the columns. (Seperating them with a comma results in an SQL syntax error.) Because the columns are concatenated Oracle seems to take an unusually long time to response when large tables are involved.

These answers are based on casual and sometimes earnest investigation but not extremely rigorous theorectical analysis. (How's that for a disclaimer :-))

-- 
============================================================================
Michael Sallwasser  | Down one path is utter dispair and hopelessness. Down 
Northrop Grumman    | the other is total destruction. Let us choose wisely.
============================================================================
Received on Thu Jun 09 1994 - 18:21:12 CEST

Original text of this message