Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
|
SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server ASP 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
 
Old April 8th, 2004, 02:27 PM
Authorized User
 
Join Date: Feb 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default SUM function with DISTINCT predicate

Hey, all.

I need some help figuring out the syntax for this SQL statement in a Stored Procedure.

Currently, I have this & it works fine returning a recordset to the ASP page:
CREATE PROCEDURE sp_LoadRequestData @CountyID Numeric (9), @MuniID Numeric (9) AS

SELECT Collectable.EntityOID, Collectable.EntityName, Collectable.CollectableOID, Collectable.CountyOID, Collectable.LocaleOID, Collectable.PayeeOID, Collectable.PayPayee, CollectableType.Collectable,
Payee.PayeeName, Payee.CertFees
FROM Collectable
INNER JOIN CollectableType ON Collectable.CollectableOID = CollectableType.CollectableOID
INNER JOIN Payee ON Collectable.PayeeOID = Payee.PayeeOID
WHERE Collectable.CountyOID = @CountyID OR Collectable.LocaleOID = @MuniID
GO

What I need to do is add a total field for the Payee.CertFees, but it needs to return one value per PayeeOID. That is, it is possible for a Payee to handle more than one job, but if they do, they only collect a fee once. So if there are two rows for the same Payee returned, I only want to count the CertFee for that Payee once.

I have tried to put SUM(Payee.CertFees) AS 'TotalCosts' in the statement, but I haven't been able to figure out how to tell it that the Payee needs to be DISTINCT.

Any ideas?

Thanks,

Dave

 
Old April 8th, 2004, 10:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can do that using Compute Clause. But I think you wont be able to interpret that in you ASP record set. I assume you would want a result something like this.

COL1 COL2 COL3
COL1 COL2 COL3
COL1 COL2 COL3
          SUM
COL1 COL2 COL3
COL1 COL2 COL3
          SUM

This can be done using something like
COMPUTE sum(Payee.CertFees) by Payee.PayeeOID

AFAIK the ASP recordset would require all the rows to have equal columns.

You cannot process it with your recordset, if it is going to be irregular.

So I would suggest you to do this in your ASP per PayeeOID, when you loop through your recordset. You can sum up the Payee.CertFees untill the PayeeOID changes, and reset it to ZERO for every PayeeOID.

Hope that helps.

Cheers!


-Vijay G





Similar Threads
Thread Thread Starter Forum Replies Last Post
sum function pcase XSLT 2 January 2nd, 2008 05:51 PM
SUM Function in ASP kylej2006 Classic ASP Basics 10 June 7th, 2007 05:46 PM
sum() function felixm_jr Reporting Services 1 April 22nd, 2007 01:59 AM
SUM Function jmss66 Classic ASP Basics 17 July 29th, 2003 08:00 AM
Need Help with the Sum Function athanatos XSLT 1 July 22nd, 2003 10:06 AM





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