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 > Database Server Software > MySQL > SQL Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-11-10, 13:03
sajanjacobk sajanjacobk is offline
Registered User
 
Join Date: Feb 2010
Posts: 3
SQL Help

I have a transaction table of which has the fields.
transactionID, StudentID,transactionType, transactionDate, Amount.
transactionId is the primary key.
this table has about 23,000 rows

There is also a table tblstudent in which all the students are listed with primary key StudentID.

Now I need to add an additional field "balance" to this table.
and the balance field needs to be populated against each row so that we get a sum(Amount) for each transactionID.

I have tried to do something like pseudocode

UPDATE tbltransaction
SET (balance= (SELECT SUM(balance) WHERE transactiondate< currentTransactionDate AND studentID= currentStudentID )
WHERE transactionID is from 1 to 23000.

I am using MySQL

Please help me with the SQL. Thank you very much in advance.
Reply With Quote
  #2 (permalink)  
Old 05-11-10, 14:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
one, could you do a SHOW CREATE TABLE for the student table table please

two, why do you have a WHERE clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-12-10, 05:54
sajanjacobk sajanjacobk is offline
Registered User
 
Join Date: Feb 2010
Posts: 3
1.
CREATE TABLE IF NOT EXISTS `tblstudent` (
`ID` int(250) NOT NULL AUTO_INCREMENT,
`admissionNumber` float NOT NULL,
`studentName` varchar(250) NOT NULL,
PRIMARY KEY (`ID`)
)

CREATE TABLE IF NOT EXISTS `tbltransaction` (
`ID` bigint(255) NOT NULL AUTO_INCREMENT,
`transactionTypeID` varchar(250) NOT NULL,
`studentID` int(250) NOT NULL,
`date` datetime NOT NULL,
`amount` float NOT NULL,
`balance` double NOT NULL,
PRIMARY KEY (`ID`)
)
2.
It is not needed

Last edited by sajanjacobk; 05-12-10 at 06:00.
Reply With Quote
  #4 (permalink)  
Old 05-12-10, 06:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
i don't understand what value you want the balance column to have
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-12-10, 07:04
sajanjacobk sajanjacobk is offline
Registered User
 
Join Date: Feb 2010
Posts: 3
This is some thing like a bank account.
And the total balance for customer as a sum account in a particular studentID.

But when the transactions increased the need to put a balance field was necessary. This helps, since from a latest single record, we get the current Balance of the studentID.
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