Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > Oracle
| Search | Today's Posts | Mark Forums Read
Oracle General Oracle database discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Oracle section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 28th, 2004, 01:52 PM
Authorized User
 
Join Date: Jan 2004
Location: Frederick, MD, USA.
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sum values by key

Hi. I have a table where I want to sum the amounts in one column in rows that share a value in a second column. This seems like a simple thing to do but I can't figure it out. I've explored using cursors and tried comparing the table to a list of distinct key_field values but it's not working for me. Any suggestions? Thanks.

amount key_field
8000 1
25000 1
1800 2
2500 2
8996 2
  #2 (permalink)  
Old April 28th, 2004, 03:12 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Justine,

The problem is not very clear; do you want to find the sum of all amounts for key_fields 1 and 2 ?

SQL> desc t3
 Name Null? Type
 --------------------------- -------- ------------------
 AMOUNT NUMBER
 KEY_FIELD NUMBER

SQL> select * from t3;

    AMOUNT KEY_FIELD
---------- ----------
      8000 1
     25000 1
      1800 2
      2500 2
      8996 2

SQL> select key_field, sum(amount) "Sum of Amounts"
  2 from t3
  3 group by key_field;

 KEY_FIELD Sum of Amounts
---------- --------------
         1 33000
         2 13296

SQL>

Cheers,
Prat




Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the Sum of all the values in a column.. jhansib4u ADO.NET 1 December 1st, 2007 09:15 PM
how can find Sum of all book values where BookType Rg2005 SQL Server 2000 2 October 8th, 2007 03:03 AM
Sum Textbox Values Snuffles ASP.NET 2.0 Basics 1 April 13th, 2007 01:36 PM
Need help to write codes to sum values sunny76 Excel VBA 1 July 6th, 2005 05:05 AM
SUM with formula values SiliconFuRy Excel VBA 3 January 7th, 2005 08:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.