If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Enhance SQL Script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-23-04, 12:59
jplsql jplsql is offline
Registered User
 
Join Date: Jan 2004
Posts: 1
Enhance SQL Script

I would like to enhance this sql script by being able to do a total amount on sum(u.amount). How can this be done?

select b.borrower#, sum(u.amount)

from burb u, borrower b

where b.borrower# = u.borrower#

and expiration_date >10226

group by b.borrower#

having sum(u.amount) > 10000
Reply With Quote
  #2 (permalink)  
Old 01-23-04, 21:46
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
select b.borrower#, sum(u.amount), (select sum(u.amount) from burb) As Total
from burb u, borrower b
where b.borrower# = u.borrower#
and expiration_date >10226
group by b.borrower#
having sum(u.amount) > 10000
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #3 (permalink)  
Old 02-02-04, 11:46
joebednarz joebednarz is offline
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
Here's an idea...

Code:
SELECT   b.borrower#, SUM (u.amount)
    FROM burb u, borrower b
   WHERE b.borrower# = u.borrower# AND expiration_date > 10226
GROUP BY b.borrower#
  HAVING SUM (u.amount) > 10000

UNION ALL

SELECT NULL, SUM (u.amount)
  FROM burb
This way, the "Total" is displayed only once and at the bottom of the list.

NOTE: You'll have to resist the temptation to do this "SELECT 'Total', SUM( u.amount )..." because when you do a UNION / UNION ALL, the column data types have to match. I'm assuming the "borrower#" is a numeric, therefore putting text in the "Total" row will give you an error.

JoeB
Reply With Quote
  #4 (permalink)  
Old 02-02-04, 12:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
both the scalar subquery and union all solutions forgot both the WHERE and the HAVING conditions, and will therefore produce incorrect results

yeah, i know, picky, picky...

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On