SQL aggregate

SQL aggregate

Post by Sidar » Thu, 20 Jan 2000 04:00:00



How do you use ORDER BY with an aggregate query?  I have a query that is
something like:

SELECT this, sum(that) AS theother
FROM atable
GROUP BY this

what I want to do is append ORDER BY to rank the summed values.

Anyone know how to do that?

Sidart

 
 
 

SQL aggregate

Post by william boule » Thu, 20 Jan 2000 04:00:00


Have you try to append at the end :
ORDER BY sum(that)



Quote:> How do you use ORDER BY with an aggregate query?  I have a query that is
> something like:

> SELECT this, sum(that) AS theother
> FROM atable
> GROUP BY this

> what I want to do is append ORDER BY to rank the summed values.

> Anyone know how to do that?

> Sidart


 
 
 

SQL aggregate

Post by Paul Goodfello » Thu, 20 Jan 2000 04:00:00


Why can't you just add an order by clause

SELECT this, sum(that) AS theother
FROM atable
GROUP BY this
ORDER BY sum(that)

As long as the field you are ordering by is in the select list you should be
fine.

Regards,

Paul


>How do you use ORDER BY with an aggregate query?  I have a query that is
>something like:

>SELECT this, sum(that) AS theother
>FROM atable
>GROUP BY this

>what I want to do is append ORDER BY to rank the summed values.

>Anyone know how to do that?

>Sidart

 
 
 

SQL aggregate

Post by Jaso » Fri, 21 Jan 2000 04:00:00


If you are using SQL Server you can't put an ORDER BY clause on
a query with an aggregate function without hacking around.
But using ADO, use a client side cursor, then set the recordset's
sort property to "theother" after opening it.
If you are not quite sure what I'm talking about, email me and
I'll send you some source code.

Jaosn


Quote:> How do you use ORDER BY with an aggregate query?  I have a query that is
> something like:

> SELECT this, sum(that) AS theother
> FROM atable
> GROUP BY this

> what I want to do is append ORDER BY to rank the summed values.

> Anyone know how to do that?

> Sidart

 
 
 

SQL aggregate

Post by Tony Buckle » Fri, 21 Jan 2000 04:00:00


You can also sort by using the position of the column..

select a,b,c,d
from f
order by 1,2

Not the best of things to do as it is all to easy to forget to change the
sort parameter if the select list changes but very good if you are building
SQL in code with variable lists.

Tony


> How do you use ORDER BY with an aggregate query?  I have a query that is
> something like:

> SELECT this, sum(that) AS theother
> FROM atable
> GROUP BY this

> what I want to do is append ORDER BY to rank the summed values.

> Anyone know how to do that?

> Sidart

 
 
 

1. SQL: Aggregate within in aggregate?

In Informix SE 7.23, 4GL 6.04
How can I in effect nest an aggreate like SELECT MAX(SUM(column))
where.....?
If I have a table

contract   char(3)
grade       char (1)
amount    integer

For a given contact I want to select the grade having the MAX  of
SUM(amount)
So if my data is:
contract        grade    amount
XYZ                A            20
XYZ                B            20
XYZ                B            19
XYZ                A            21
XYZ                C            30

I want the SQL statement to return me the grade value of "A" since the
SUM(amount) of all A grade rows is 41 and the SUM(amount) of all B grade
rows is 39 and C = 30
Please help me with the syntax.

Thanks in advance
Matt Brickley

2. FAQ for Sybase group

3. ANSI SQL vs. Jet SQL / Aggregate functions

4. DTS Designer Icons

5. SQL, aggregate functions, running total question

6. Question about _PDSETUP and Print Drivers

7. Ingres SQL - Aggregate functions with updates

8. Distributed Join

9. SQL Aggregates

10. SQL Aggregate for Standard Deviation?

11. SQL Aggregate commands

12. SQL - aggregate functions and groups

13. Newbie: SQL Aggregate Functions????