Solved

SQL - ORDER BY SUM()

Posted on 2000-03-03
6
288 Views
Last Modified: 2010-04-04
I use delphi with interbase5.
I found out that it is inpossible to do a query like this:

select A.name, sum(B.value) from A,B
where A.name=B.name
group by A.name
order by sum(B.value)

You can't "order by sum()"

In Microsoft accces this is (via ODBC) very easy.

Does anyone has a solution for this or a workaround ?
0
Comment
Question by:RichardBorst
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:simonet
ID: 2608663
Create a VIEW that contains the first select:

CREATE VIEW MyFirstView AS
  select A.name as Name, sum(B.value) as SumOfB from A,B
  where A.name=B.name
  group by A.name

(actual syntax may vary, since I am using SQL Server)

Now, in order for you to select everything sorting by the aggregate field, treat the VIEW as a regular table (all data is dynamic):

SELECT * FROM MyFirstView
ORDER BY SumOfB


Another option is to have a select inside another select (pretty much as a view), but I don't know if Interbase supports it. Anyway, it's like this:

SELECT Name, SumOfB FROM
   (select A.name as Name, sum(B.value) as SumOfB from A,B
  where A.name=B.name
  group by A.name
)
ORDER BY SumOfB


This doesn't work in all cases, but there's a pretty good chance IB supports it. Anyhow, the first approach (creating a VIEW) is still your best shot.

Yours,

Alex



0
 

Author Comment

by:RichardBorst
ID: 2615399
Sorry, But both your solutions are not supported in interbase.

1.In a view I can't use a 'group by'
and
2.I can't 'select from (select ...'

I think that is a limitation of interbase.

The only 'solution' I found out is  to create a second table based on the query with the 'group by' and then open the table and with an order by the sum-field.

Unfortunalely this is a time-consuming operation.
0
 

Expert Comment

by:johnstoned
ID: 2628634
I'm not sure about interbase 5, but this statement works in SQL Server7,

SELECT name, SUM(value) AS Total
FROM b
WHERE name IN
        (SELECT DISTINCT name
      FROM a)
GROUP BY name
ORDER BY Total


Dave.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RichardBorst
ID: 2703589
sorry johnstoned but order by total does not work ininterbase
0
 
LVL 1

Accepted Solution

by:
DValery earned 200 total points
ID: 2708148
Hi, RichardBorst

It's easy :o)

select A.name, sum(B.value)
from A,B
where A.name=B.name
group by A.name
order by 2
0
 

Author Comment

by:RichardBorst
ID: 2799136
sorry for the delay.

Your solution works !
In fact, it's so simple, but I could not find this in any helpfile or book.

Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question