Solved

SQL - ORDER BY SUM()

Posted on 2000-03-03
6
283 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
6 Comments
 
LVL 15

Expert Comment

by:simonet
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:RichardBorst
Comment Utility
sorry johnstoned but order by total does not work ininterbase
0
 
LVL 1

Accepted Solution

by:
DValery earned 200 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now