0

I have a table defined as bellow:

sym ="C""MS""MS""MS""IBM""IBM""C""C""C"$symbol;
price= 49.6 29.46 29.52 30.02 174.97 175.23 50.76 50.32 51.29;
qty = 2200 1900 2100 3200 6800 5400 1300 2500 8800;
timestamp = [09:34:07,09:36:42,09:36:51,09:36:59,09:32:47,09:35:26,09:34:16,09:34:26,09:38:12];
t1 = table(timestamp, sym, qty, price);

Then, the data in t1 is

like this

I want to calculate stock return for each firm,and get results like the following,

sym timestamp price  ret
--- --------- ------ ---------
C   09:34:07  49.6
C   09:34:16  50.76  0.023387
C   09:34:26  50.32  -0.008668
C   09:38:12  51.29  0.019277
IBM 09:32:47  174.97
IBM 09:35:26  175.23 0.001486
MS  09:36:42  29.46
MS  09:36:51  29.52  0.002037
MS  09:36:59  30.02  0.016938

How to write SQL statements to satisfy the requirement?

j.doe
  • 662
  • 4
  • 19
Wale
  • 1
  • 8

1 Answers1

0

Use CONTEXT BY clause to solve your problem.

The context by clause will split data into groups similar to the group by clause. However, one can apply a vector function rather than an aggregated function to each group.

select sym, timestamp, price, ratios(price) - 1 as ret from t1 context by sym csort timestamp
Davis Zhou
  • 353
  • 4
  • 6