Connecting Tech Pros Worldwide Help | Site Map

2 group by in a single query

Jai
Guest
 
Posts: n/a
#1: Jul 20 '05
Say the following are the columns of a table
A B C D E F

Can a aggregate function like sum be applied to A like sum(a) and then
order by b and c similarly aggregate function on d and group by e and
f using a single query...


Regards,
Jai
David Portas
Guest
 
Posts: n/a
#2: Jul 20 '05

re: 2 group by in a single query


I'm not sure why you think you need two GROUP BYs. What do you expect to be
the difference between GROUP BY x followed by GROUP BY y rather than just
GROUP BY y? You can of course nest GROUP BY queries using a derived table in
the FROM clause. Nested aggregates can be used to count distinct values
within a group, in a similar way to COUNT(DISTINCT x):

SELECT A, COUNT(*)
FROM
(SELECT A,B
FROM foo
GROUP BY A,B) AS X
GROUP BY A

If you need more help, please post a CREATE TABLE statement for your table
and post some sample data as INSERT statements.

--
David Portas
------------
Please reply only to the newsgroup
--


Jai
Guest
 
Posts: n/a
#3: Jul 20 '05

re: 2 group by in a single query


Thanks a lot...
Ur example helped me a lot...
This was exactly what I was looking for...
Regards,
Jai

"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message news:<huednWqrTsLicPKiRVn-uQ@giganews.com>...[color=blue]
> I'm not sure why you think you need two GROUP BYs. What do you expect to be
> the difference between GROUP BY x followed by GROUP BY y rather than just
> GROUP BY y? You can of course nest GROUP BY queries using a derived table in
> the FROM clause. Nested aggregates can be used to count distinct values
> within a group, in a similar way to COUNT(DISTINCT x):
>
> SELECT A, COUNT(*)
> FROM
> (SELECT A,B
> FROM foo
> GROUP BY A,B) AS X
> GROUP BY A
>
> If you need more help, please post a CREATE TABLE statement for your table
> and post some sample data as INSERT statements.[/color]
Closed Thread