0

我想知道是否有人可以帮助我构建一个查询或 php 代码,给出下表可以在“所需输出”列上产生输出。

基本上,查询或 php 代码需要按交易品种对金融波动率进行求和,并在每次计算或开仓为 0 时显示结果。

 tid    | time      | symbol | openposition  | financialvol | desired output
 87646  | 10:29:38  | CTIP3  | 400           | 8744.00      |
 87645  | 10:29:47  | CTIP3  | 800           | 8732.00      |
 87644  | 10:29:52  | CTIP3  | 1000          | 4366.00      |
 87643  | 10:30:02  | CTIP3  | 1200          | 4368.00      |
 87642  | 10:30:06  | CTIP3  | 1400          | 4364.00      |
 87639  | 10:31:57  | CTIP3  | 1200          | -4382.00     |
 87638  | 10:32:04  | CTIP3  | 1000          | -4372.00     |
 87637  | 10:32:08  | CTIP3  | 800           | -4362.00     |
 87636  | 10:32:14  | CTIP3  | 600           | -4388.00     |
 87635  | 10:32:40  | CTIP3  | 0             | -13098.00    | -28
 87647  | 10:28:54  | PCAR4  | 400           | 40480.00     |
 87641  | 10:31:30  | PCAR4  | 200           | -20246.00    |
 87640  | 10:31:41  | PCAR4  | 0             | -20340.00    | -106
 87651  | 10:27:42  | TIMP3  | 1500          | 12345.00     |
 87650  | 10:27:48  | TIMP3  | 11500         | 82300.00     |
 87649  | 10:28:09  | TIMP3  | 1500          | -82600.00    |
 87648  | 10:28:23  | TIMP3  | 0             | -12345.00    | -300
 87657  | 10:26:44  | VIVT4  | -500          | -23710.00    |
 87656  | 10:26:53  | VIVT4  | 0             | 23700.00     | -10
 87655  | 10:26:59  | VIVT4  | -500          | -23740.00    |
 87653  | 10:27:00  | VIVT4  | -1000         | -23740.00    |
 87654  | 10:27:00  | VIVT4  | -1500         | -23740.00    |
 87652  | 10:27:24  | VIVT4  | 0             | 71055.00     | -175

感谢大家的关注和尝试!

4

2 回答 2

0

我相信你正在寻找这样的东西

为此,您需要将表格排序为:order by time asc

于 2013-10-04T20:19:42.760 回答
0
SELECT x.*
     , CASE WHEN x.openposition = 0 THEN SUM(y.financialvol) ELSE '' END desired
  FROM my_table x 
  JOIN my_table y 
    ON y.symbol = x.symbol AND y.time <= x.time 
 GROUP 
    BY x.symbol, x.time;
于 2013-10-05T14:38:30.937 回答