juddymarch
Technical User
- Aug 10, 2007
- 17
Hi, I'm new to MYSQL to please bear with me.
I have a table called tblEquityDetailDaily:
fldRecordID fldEquityDateID fldDate fldLiveShares
1 55 1/1/08 100
2 55 2/1/08 200
3 60 1/1/08 500
4 60 2/1/08 0
5 60 3/1/08 500
6 70 1/1/08 0
7 70 2/1/08 0
What I'm trying to do is if the liveshares are equal to 0 I want to update it to the previous value where fldEquityDateID is the same so record 4 would be updated to 500 as the previous value was on the 1/1/08. I have written a stored procedure which works but will fall over if the first value is 0 or all of the values are 0 for a particular equitydateid.
-------------------------------------------------------
CREATE Temporary TABLE TempA
SELECT * FROM tblEquityDetailDaily WHERE fldLiveShares <> 0;
UPDATE tblequitydetaildaily SET tblequitydetaildaily.fldLiveShares = (
SELECT fldLiveShares FROM TempA
WHERE TempA.fldDate < tblequitydetaildaily.fldDate AND
TempA.fldEquityDateID = tblEquityDetailDaily.fldEquityDateID AND TempA.fldLiveshares <> 0
ORDER BY TempA.fldDate DESC
LIMIT 1)
WHERE tblequitydetaildaily.fldLiveShares=0;
DROP temporary TABLE TempA;
--------------------------------------------------------
I am using a temp table as i couldn't get it to work using the same table or an alias. Just wondering how can I prevent it from falling over if the first record is 0 or all records are 0 for the same equitydateid. I have done a few searches and can't seem to find a solution with out going into cursors etc.
Any help would be greatly appreciated.
Thanks
Justin
I have a table called tblEquityDetailDaily:
fldRecordID fldEquityDateID fldDate fldLiveShares
1 55 1/1/08 100
2 55 2/1/08 200
3 60 1/1/08 500
4 60 2/1/08 0
5 60 3/1/08 500
6 70 1/1/08 0
7 70 2/1/08 0
What I'm trying to do is if the liveshares are equal to 0 I want to update it to the previous value where fldEquityDateID is the same so record 4 would be updated to 500 as the previous value was on the 1/1/08. I have written a stored procedure which works but will fall over if the first value is 0 or all of the values are 0 for a particular equitydateid.
-------------------------------------------------------
CREATE Temporary TABLE TempA
SELECT * FROM tblEquityDetailDaily WHERE fldLiveShares <> 0;
UPDATE tblequitydetaildaily SET tblequitydetaildaily.fldLiveShares = (
SELECT fldLiveShares FROM TempA
WHERE TempA.fldDate < tblequitydetaildaily.fldDate AND
TempA.fldEquityDateID = tblEquityDetailDaily.fldEquityDateID AND TempA.fldLiveshares <> 0
ORDER BY TempA.fldDate DESC
LIMIT 1)
WHERE tblequitydetaildaily.fldLiveShares=0;
DROP temporary TABLE TempA;
--------------------------------------------------------
I am using a temp table as i couldn't get it to work using the same table or an alias. Just wondering how can I prevent it from falling over if the first record is 0 or all records are 0 for the same equitydateid. I have done a few searches and can't seem to find a solution with out going into cursors etc.
Any help would be greatly appreciated.
Thanks
Justin