Solved

Is there a function that I can use in MSSQL that will make one of my fields have a running total as it's value?

Posted on 2008-06-23
5
174 Views
Last Modified: 2010-03-20
I have a database for a 'ClassSignUp'.  One of the fields is the 'TotalSeats' in each class.  
I have another table of 'Attendees' . This table contains a foreign key field of the 'ClassSignUp'.  

I want each row in the 'ClassSignUp' to show the 'TotalSeats' as entered and I also want to calculate the 'RemainingSeats' to show in my grid or table.  

For example, if the 'ClassSignUp' table has a 'class' that has been assigned 30 'TotalSeats', I want to also look into the 'Attendees' table and do a count('EventID') and subtract that number from the 'TotalSeats' column to show the 'RemainingSeats' for row table.

Thanks for any help!
0
Comment
Question by:ScottLakey
5 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 84 total points
ID: 21849476
Can you do it...yes.

Is it easy to do or fast?  No.

Here is an article I wrote a long time ago as to how you can do it with a user defined function...

http://www.sqlservercentral.com/articles/User-Defined+functions/complexcomputedcolumns/2397/
0
 
LVL 11

Assisted Solution

by:aaronakin
aaronakin earned 83 total points
ID: 21849548
Chap's right.  You'll have to write your own function for this and add it as a computed column.
CREATE FUNCTION dbo.GetRemainingSeats
(
	@EventID		INT,
	@TotalSeats		INT
)
RETURNS INT
AS
BEGIN
  DECLARE @RemainingSeats INT
  SELECT @RemainingSeats = @TotalSeats - COUNT(EventID) FROM dbo.Attendees WHERE EventID = @EventID
  RETURN @RemainingSeats
END
 
ALTER TABLE dbo.ClassSignUp
  ADD RemainingSeats AS dbo.GetRemainingSeats(EventID, TotalSeats)

Open in new window

0
 
LVL 10

Assisted Solution

by:Marcjev
Marcjev earned 83 total points
ID: 21850467
I think it can be done without a function.
Assume yoy have a certain ordering in your attendee table (example on attendeeid)
and and eventtable with an event and a totalseats table.

DECLARE @EventId Int
SELECT @EventId = 1
SELECT Att.AttendeeId, EventTable.TotalSeats,  
   EventTable.TotalSeats - ( SELECT count(EventId)
			  FROM Attendees Att2 
			  WHERE EventId = @EventId
			  AND Att2.AttendeeId <= Att.AttendeeId) 
                             as RunningTotal
FROM Attendees Att
JOIN EventTable ON Eventable.EventId = @EventId
ORDER BY Att.AttendeeId

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tempdb log keep growing 7 35
SQL Server / Update DB? 22 38
Transact SQL - Frequency of Length of Distinct Values 3 26
Dynamic SQL select query 4 38
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

821 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