Challange: How do I sum data from the same table in an SQL query???

Challange: How do I sum data from the same table in an SQL query???

Post by Brad » Mon, 04 Oct 1999 04:00:00



Hmm. I thought this would be easy.

In Microsoft Access I have a set of data that looks like this:

AccountNum        Amount
2000A                    12
2000B                    3
4000A                    50
4000B                    40

I would like to 'add-up' accounts of the same number (eg: combine the "A"
accounts with the "B" accounts) in an SQL query. For example, the resulting
table would look like:

NewAccountNum        NewAmount
2000                            15
4000                             90

I've been trying to use a sub query that goes seomthing like:

SELECT
Amount +
    (SELECT Amount FROM table WHERE AccountNum = Left
    (AccountNum,LEN(AccountNum)-1) & "B")

AS NewAmount

FROM table

WHERE RIGHT(AccountNum,1) <> "B"

But of course my sub-query returns multple values.

Thanks!

 
 
 

Challange: How do I sum data from the same table in an SQL query???

Post by John Peterso » Mon, 04 Oct 1999 04:00:00


Brad,

You might try something like:

select substring(AccountNum, 1, datalength(AccountNum) - 1),
       sum(Amount)
  from YourTable
 group by substring(AccountNum, 1, datalength(AccountNum) - 1)

(BTW:  The substring might be overkill, but I wasn't sure how your
AccountNum data was structured.)

Hope this helps!

John Peterson


> Hmm. I thought this would be easy.

> In Microsoft Access I have a set of data that looks like this:

> AccountNum        Amount
> 2000A                    12
> 2000B                    3
> 4000A                    50
> 4000B                    40

> I would like to 'add-up' accounts of the same number (eg: combine the "A"
> accounts with the "B" accounts) in an SQL query. For example, the
resulting
> table would look like:

> NewAccountNum        NewAmount
> 2000                            15
> 4000                             90

> I've been trying to use a sub query that goes seomthing like:

> SELECT
> Amount +
>     (SELECT Amount FROM table WHERE AccountNum = Left
>     (AccountNum,LEN(AccountNum)-1) & "B")

> AS NewAmount

> FROM table

> WHERE RIGHT(AccountNum,1) <> "B"

> But of course my sub-query returns multple values.

> Thanks!


 
 
 

1. Real challange..SQL query problem

Hi my name is mikael and have a problem with
my sajt. I'm going to implement a new future but
it's too difficult for me to construct the nessesery
sql-query. I have set up a page there i trying
to explain my problem in detail.
Pleas look here to see if you can help me.
Regards Mikael
http://www.hyresbytet.se/query.html

2. VB/Access Date/Time Help

3. Help - Simple SQL query doing full table scans

4. Using Report Gen. -- 2 records on 1 detail line

5. query challange!!

6. Progress VMS RMS gateway

7. Query writting challange

8. Designer V 2.1 Oracle 8.0.4

9. Restore Data from Previous Backup - ( A Challange)

10. Data Enviroment Problems------A real challange

11. data movement from one table to several tables using a SQL Query

12. Cannot read Watcom SQL Data tables using Microsoft Query/odbc Only System tables

13. Need help with joined table sum query