Connecting Tech Pros Worldwide Help | Site Map

2 group by in a single query

  #1  
Old July 20th, 2005, 02:34 AM
Jai
Guest
 
Posts: n/a
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, 02:35 AM
David Portas
Guest
 
Posts: n/a

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, 02:36 AM
Jai
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Doing Summation on multiple criterias on the same column in a singlequery Darsin answers 25 April 3rd, 2008 11:15 PM
Summary Table - Optimizer not using if ORDER BY in query Rob K. answers 0 November 23rd, 2005 04:07 AM
SQL query - single text value from group by mike answers 2 November 23rd, 2005 02:02 AM
How to Date Filter on a Group By Month query Tony Miller answers 6 November 13th, 2005 01:02 PM