Pergunta

I have a table, open_price, close_price and profit columns. I need to get 4 column (balance) where balance value will be equal previous balance row value + currenct row profit. Please check my description below:

open_date                   close_date          profit  
2012-08-14 12:02:46     2012-08-14 14:39:38     14.50   
2012-08-14 14:43:08     2012-08-14 15:41:58     -14.50  
2012-08-14 15:41:58     2012-08-14 16:09:15     -29.00  
2012-08-14 16:09:15     2012-08-14 16:15:04     1.20    
2012-08-14 16:26:19     2012-08-14 16:40:37     14.50   
2012-08-15 08:25:56     2012-08-15 08:37:37     -1.20   
2012-08-15 09:06:26     2012-08-15 12:57:26     -14.50  
2012-08-15 12:57:26     2012-08-15 13:08:43     29.00   
2012-08-15 13:15:06     2012-08-15 13:20:20     14.50   
2012-08-15 13:22:15     2012-08-15 15:23:10     -5.40

I have a request and get new table:

SELECT open_date, close_date, profit, ( 1000 + profit ) AS balance
FROM users_signals
ORDER BY open_date ASC
LIMIT 0 , 30

open_date Ascending     close_date          profit  balance
2012-08-14 12:02:46     2012-08-14 14:39:38     14.50   1014.50
2012-08-14 14:43:08     2012-08-14 15:41:58     -14.50  985.50
2012-08-14 15:41:58     2012-08-14 16:09:15     -29.00  971.00
2012-08-14 16:09:15     2012-08-14 16:15:04     1.20    1001.20
2012-08-14 16:26:19     2012-08-14 16:40:37     14.50   1014.50
2012-08-15 08:25:56     2012-08-15 08:37:37     -1.20   998.80
2012-08-15 09:06:26     2012-08-15 12:57:26     -14.50  985.50
2012-08-15 12:57:26     2012-08-15 13:08:43     29.00   1029.00
2012-08-15 13:15:06     2012-08-15 13:20:20     14.50   1014.50
2012-08-15 13:22:15     2012-08-15 15:23:10     -5.40   994.60

How can I get this table:

open_date Ascending     close_date          profit  balance
2012-08-14 12:02:46     2012-08-14 14:39:38     14.50   1014.50
2012-08-14 14:43:08     2012-08-14 15:41:58     -14.50  1000     //1014.50+-14.50
2012-08-14 15:41:58     2012-08-14 16:09:15     -29.00  971.00   //1000+-29.00 
2012-08-14 16:09:15     2012-08-14 16:15:04     1.20    972.20   //971.00+1.20
2012-08-14 16:26:19     2012-08-14 16:40:37     14.50   986.70   //972.20+14.50
2012-08-15 08:25:56     2012-08-15 08:37:37     -1.20   985.50   //986.70+-1.20 
2012-08-15 09:06:26     2012-08-15 12:57:26     -14.50  971.00   //985.5+-14.50  
2012-08-15 12:57:26     2012-08-15 13:08:43     29.00   1000.00  //971.00+29.00 
2012-08-15 13:15:06     2012-08-15 13:20:20     14.50   1014.50  //1000.00+14.50
2012-08-15 13:22:15     2012-08-15 15:23:10     -5.40   1009.1   //1014.50-5.40 
Foi útil?

Solução

The following will work ::

SET @bal=1000;
SELECT open_date, close_date, profit,@bal := @bal+profit AS balance
FROM users_signals
ORDER BY open_date ASC
LIMIT 0 , 30

This will keep updating @bal

Outras dicas

Use a subquery like this:

SELECT open_date, close_date, profit, 
    (1000 + (SELECT SUM(us.profit) 
             FROM user_signals us 
             WHERE us.open_date <= users_signals.open_date)) AS balance
FROM users_signals
ORDER BY open_date ASC
LIMIT 0 , 30
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top