Connecting Tech Pros Worldwide Help | Site Map

2 group by in a single query

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 01:34 AM
Jai
Guest
 
Posts: n/a
Default 2 group by in a single query

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

  #2  
Old July 20th, 2005, 01:35 AM
David Portas
Guest
 
Posts: n/a
Default 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
--


  #3  
Old July 20th, 2005, 01:36 AM
Jai
Guest
 
Posts: n/a
Default 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]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.