From: du_bing on
On Mar 12, 1:20 pm, Tonkuma <tonk...(a)fiberbit.net> wrote:
> >> to get total count from each section first and then get the total counts of all sections <<
>
> Get "count from each section" and "total counts" at once.
>
> SELECT CASE GROUPING(section)
>        WHEN 1 THEN
>             'All sctions'
>        ELSE section
>        END  AS section
>      , SUM(used) AS count
>   FROM (
>        SELECT 'Section one'
>             , COUNT(*)
>          FROM ur.agncy
>         WHERE agncy_id BETWEEN '2283' AND '2329'
>        UNION ALL
>        SELECT 'Section two'
>             , COUNT(*)
>          FROM ur.agncy
>         WHERE agncy_id BETWEEN '2922' AND '2994'
>        ) AS q (section , used)
>  GROUP BY
>        ROLLUP(section)
> ;

Thanks very much of the help. Also really appreciated providing other
options!

Bing
From: Tonkuma on
On Mar 13, 4:20 am, Tonkuma <tonk...(a)fiberbit.net> wrote:
> >> to get total count from each section first and then get the total counts of all sections <<
>
> Get "count from each section" and "total counts" at once.
>
Another example(returns one row):
SELECT COUNT(*) AS "All sections"
, COUNT(CASE
WHEN agncy_id BETWEEN '2283' AND '2329' THEN 0
END
) AS "Section one"
, COUNT(CASE
WHEN agncy_id BETWEEN '2922' AND '2994' THEN 0
END
) AS "Section two"
FROM ur.agncy
WHERE agncy_id BETWEEN '2283' AND '2329'
OR agncy_id BETWEEN '2922' AND '2994'
;