I want to ask a question on how to call on the specific columns that only contains an even numbers.
On my previous questions :How to make all the rows data drop the similar data and multiplying float numbers.
df2 =df['hlogUs_dB'].str.split('[,:]',expand = True)
df2 = data.drop(["0"])
df2
0 1 2 3 4 5 6 7 8 9 ... 276 277 278 279 280 281 282 283 284 285
0 109 -3.4 110 -3.4 111 -3.4 112 -3.5 113 -3.5 ... 343 -4.3 344 -4.3 345 -4.2 346 -4.2 347 -4.2
1 109 -3.5 110 -3.5 111 -3.4 112 -3.4 113 -3.4 ... 343 -4.1 344 -4.2 345 -4.4 346 -4.4 347 -4.2
2 109 -3.7 110 -3.7 111 -3.8 112 -3.8 113 -3.8 ... 343 -4.2 344 -4.3 345 -4.3 346 -4.3 347 -4.3
3 109 -3.5 110 -3.6 111 -3.6 112 -3.6 113 -3.7 ... 343 -4.1 344 -4.1 345 -4.1 346 -4.1 347 -4.1
4 109 -3.7 110 -3.8 111 -3.8 112 -3.8 113 -3.8 ... 343 -4.2 344 -4.2 345 -4.2 346 -4.2 347 -4.3
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
165 109 -5.2 110 -5.3 111 -5.5 112 -5.7 113 -5.9 ... 343 -5.4 344 -5.3 345 -5.2 346 -5.1 347 -5.1
166 109 -5.5 110 -5.6 111 -5.8 112 -6.1 113 -6.3 ... 343 -5.5 344 -5.4 345 -5.3 346 -5.2 347 -5.2
167 109 -6.0 110 -6.2 111 -6.4 112 -6.7 113 -7.1 ... 343 -4.9 344 -4.9 345 -4.9 346 -4.9 347 -4.9
168 109 -5.4 110 -5.5 111 -5.7 112 -5.9 113 -6.2 ... 343 -5.9 344 -5.7 345 -5.7 346 -5.6 347 -5.6
169 109 -5.9 110 -6.1 111 -6.4 112 -6.6 113 -7.0 ... 343 -5.7 344 -5.7 345 -5.7 346 -5.6 347 -5.6
170 rows × 286 columns
My question is how to called out on even number without using a manual way of typing all the even number of the head of columns.
such as:
df2[[0,2,4]]*= 2
I am currently stuck on the ideas on making the conditional on the header columns. I want to call on even numbers only. I hope to find a suitable solutions on the questions. Thank you in advance.
We can select all columns based on whose modulus 2 is 0 (even):
even_cols = df.columns[(df.columns % 2) == 0]
even_cols:
Int64Index([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18,
...
266, 268, 270, 272, 274, 276, 278, 280, 282, 284],
dtype='int64', length=143)
Then operations can use the newly created index:
df[even_cols] *= 2
df:
0 1 2 3 4 5 6 ... 279 280 281 282 283 284 285
0 198 78 122 16 146 8 124 ... 8 102 61 168 52 148 25
1 18 31 78 59 44 80 116 ... 75 124 51 4 96 38 7
2 152 66 112 0 114 31 172 ... 18 186 19 84 29 36 0
3 80 99 152 25 34 31 106 ... 59 190 33 68 31 66 83
4 192 95 48 95 130 14 8 ... 6 74 79 40 46 198 65
.. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
165 170 58 110 75 196 11 50 ... 54 46 53 146 62 30 48
166 54 8 148 25 174 40 114 ... 6 180 32 94 44 142 16
167 42 48 48 31 126 60 86 ... 11 128 10 162 67 142 13
168 54 37 70 2 128 38 134 ... 85 166 40 142 57 54 52
169 164 41 146 40 64 44 28 ... 83 90 86 188 23 38 35
If we need every other column instead of columns based on numeric value, we can set the step of slicing to create a list of columns:
every_other_column = df.columns[::2]
df[every_other_column] *= 2
Or simply modify the DataFrame without creating a list of columns:
df.loc[:, ::2] *= 2
Sample DataFrame:
import numpy as np
import pandas as pd
np.random.seed(5)
df = pd.DataFrame(np.random.randint(0, 100, (170, 286)))
print(df)
df:
0 1 2 3 4 5 6 ... 279 280 281 282 283 284 285
0 99 78 61 16 73 8 62 ... 8 51 61 84 52 74 25
1 9 31 39 59 22 80 58 ... 75 62 51 2 96 19 7
2 76 66 56 0 57 31 86 ... 18 93 19 42 29 18 0
3 40 99 76 25 17 31 53 ... 59 95 33 34 31 33 83
4 96 95 24 95 65 14 4 ... 6 37 79 20 46 99 65
.. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
165 85 58 55 75 98 11 25 ... 54 23 53 73 62 15 48
166 27 8 74 25 87 40 57 ... 6 90 32 47 44 71 16
167 21 48 24 31 63 60 43 ... 11 64 10 81 67 71 13
168 27 37 35 2 64 38 67 ... 85 83 40 71 57 27 52
169 82 41 73 40 32 44 14 ... 83 45 86 94 23 19 35
How to separate all of the columns?
df= df[['hlogUs_dB','hlogDs_dB']]
df
hlogUs_dB hlogDs_dB
0 109:-3.4,110:-3.4,111:-3.4,112:-3.5,113:-3.5,1... 5:-2.5,6:-2.5,7:-2.1,8:-2.0,9:-2.0,10:-2.0,11:...
1 109:-3.5,110:-3.5,111:-3.4,112:-3.4,113:-3.4,1... 5:-2.1,6:-2.0,7:-1.8,8:-1.8,9:-1.8,10:-1.8,11:...
2 109:-3.7,110:-3.7,111:-3.8,112:-3.8,113:-3.8,1... 5:-2.1,6:-2.0,7:-1.8,8:-1.8,9:-1.8,10:-1.8,11:...
3 109:-3.5,110:-3.6,111:-3.6,112:-3.6,113:-3.7,1... 5:-2.5,6:-2.5,7:-2.1,8:-2.0,9:-2.0,10:-2.0,11:...
4 109:-3.7,110:-3.8,111:-3.8,112:-3.8,113:-3.8,1... 5:-2.5,6:-2.5,7:-2.1,8:-2.1,9:-2.0,10:-2.1,11:...
... ... ...
165 109:-5.2,110:-5.3,111:-5.5,112:-5.7,113:-5.9,1... 5:-2.5,6:-2.5,7:-2.1,8:-2.1,9:-2.1,10:-2.2,11:...
166 109:-5.5,110:-5.6,111:-5.8,112:-6.1,113:-6.3,1... 5:-2.8,6:-2.7,7:-2.5,8:-2.5,9:-2.3,10:-2.5,11:...
167 109:-6.0,110:-6.2,111:-6.4,112:-6.7,113:-7.1,1... 5:-2.6,6:-2.5,7:-2.2,8:-2.2,9:-2.2,10:-2.3,11:...
168 109:-5.4,110:-5.5,111:-5.7,112:-5.9,113:-6.2,1... 5:-3.0,6:-3.0,7:-2.6,8:-2.5,9:-2.5,10:-2.5,11:...
169 109:-5.9,110:-6.1,111:-6.4,112:-6.6,113:-7.0,1... 5:-2.7,6:-2.5,7:-2.3,8:-2.2,9:-2.3,10:-2.3,11:...
170 rows × 2 columns
<After that I split using delimiter for only hlogUs_dB/>
df2 =df['hlogUs_dB'].str.split('[,:]',expand = True)
df2 = data.drop(["0"])
df2
The result :
0 1 2 3 4 5 6 7 8 9 ... 276 277 278 279 280 281 282 283 284 285
0 109 -3.4 110 -3.4 111 -3.4 112 -3.5 113 -3.5 ... 343 -4.3 344 -4.3 345 -4.2 346 -4.2 347 -4.2
1 109 -3.5 110 -3.5 111 -3.4 112 -3.4 113 -3.4 ... 343 -4.1 344 -4.2 345 -4.4 346 -4.4 347 -4.2
2 109 -3.7 110 -3.7 111 -3.8 112 -3.8 113 -3.8 ... 343 -4.2 344 -4.3 345 -4.3 346 -4.3 347 -4.3
3 109 -3.5 110 -3.6 111 -3.6 112 -3.6 113 -3.7 ... 343 -4.1 344 -4.1 345 -4.1 346 -4.1 347 -4.1
4 109 -3.7 110 -3.8 111 -3.8 112 -3.8 113 -3.8 ... 343 -4.2 344 -4.2 345 -4.2 346 -4.2 347 -4.3
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
165 109 -5.2 110 -5.3 111 -5.5 112 -5.7 113 -5.9 ... 343 -5.4 344 -5.3 345 -5.2 346 -5.1 347 -5.1
166 109 -5.5 110 -5.6 111 -5.8 112 -6.1 113 -6.3 ... 343 -5.5 344 -5.4 345 -5.3 346 -5.2 347 -5.2
167 109 -6.0 110 -6.2 111 -6.4 112 -6.7 113 -7.1 ... 343 -4.9 344 -4.9 345 -4.9 346 -4.9 347 -4.9
168 109 -5.4 110 -5.5 111 -5.7 112 -5.9 113 -6.2 ... 343 -5.9 344 -5.7 345 -5.7 346 -5.6 347 -5.6
169 109 -5.9 110 -6.1 111 -6.4 112 -6.6 113 -7.0 ... 343 -5.7 344 -5.7 345 -5.7 346 -5.6 347 -5.6
170 rows × 286 columns
After that I want to drop the same number that appear only on even columns. I manage to found the solutions but somehow or rather, it does not suit my preference.
df2.drop(columns=[0,2,4,6,8,9,10,12,14,16,18,20,22,24,26,28,30,32,34,36])
df2
the output:
0 1 2 3 4 5 6 7 8 9 ... 276 277 278 279 280 281 282 283 284 285
0 109 -3.4 110 -3.4 111 -3.4 112 -3.5 113 -3.5 ... 343 -4.3 344 -4.3 345 -4.2 346 -4.2 347 -4.2
1 109 -3.5 110 -3.5 111 -3.4 112 -3.4 113 -3.4 ... 343 -4.1 344 -4.2 345 -4.4 346 -4.4 347 -4.2
2 109 -3.7 110 -3.7 111 -3.8 112 -3.8 113 -3.8 ... 343 -4.2 344 -4.3 345 -4.3 346 -4.3 347 -4.3
3 109 -3.5 110 -3.6 111 -3.6 112 -3.6 113 -3.7 ... 343 -4.1 344 -4.1 345 -4.1 346 -4.1 347 -4.1
4 109 -3.7 110 -3.8 111 -3.8 112 -3.8 113 -3.8 ... 343 -4.2 344 -4.2 345 -4.2 346 -4.2 347 -4.3
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
165 109 -5.2 110 -5.3 111 -5.5 112 -5.7 113 -5.9 ... 343 -5.4 344 -5.3 345 -5.2 346 -5.1 347 -5.1
166 109 -5.5 110 -5.6 111 -5.8 112 -6.1 113 -6.3 ... 343 -5.5 344 -5.4 345 -5.3 346 -5.2 347 -5.2
167 109 -6.0 110 -6.2 111 -6.4 112 -6.7 113 -7.1 ... 343 -4.9 344 -4.9 345 -4.9 346 -4.9 347 -4.9
168 109 -5.4 110 -5.5 111 -5.7 112 -5.9 113 -6.2 ... 343 -5.9 344 -5.7 345 -5.7 346 -5.6 347 -5.6
169 109 -5.9 110 -6.1 111 -6.4 112 -6.6 113 -7.0 ... 343 -5.7 344 -5.7 345 -5.7 346 -5.6 347 -5.6
170 rows × 286 columns
still show the same as before, I just want the odd columns to be multiply with 8 and float 4.3125. Then later the data will replace on the same columns, that was my roughly ideas.
df2*4.3125
the results contain error.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_arithmetic_op(left, right, op, str_rep)
148 try:
--> 149 result = expressions.evaluate(op, str_rep, left, right)
150 except TypeError:
~\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in evaluate(op, op_str, a, b, use_numexpr)
207 if use_numexpr:
--> 208 return _evaluate(op, op_str, a, b)
209 return _evaluate_standard(op, op_str, a, b)
~\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_numexpr(op, op_str, a, b)
120 if result is None:
--> 121 result = _evaluate_standard(op, op_str, a, b)
122
~\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_standard(op, op_str, a, b)
69 with np.errstate(all="ignore"):
---> 70 return op(a, b)
71
TypeError: can't multiply sequence by non-int of type 'float'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-24-424060d3aad6> in <module>
----> 1 df2*4.3125
~\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in f(self, other, axis, level, fill_value)
717 self = self.fillna(fill_value)
718
--> 719 new_data = dispatch_to_series(self, other, op, str_rep)
720 return self._construct_result(new_data)
721
~\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in dispatch_to_series(left, right, func, str_rep, axis)
376 # Get the appropriate array-op to apply to each block's values.
377 array_op = get_array_op(func, str_rep=str_rep)
--> 378 bm = left._data.apply(array_op, right=right)
379 return type(left)(bm)
380
~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, filter, **kwargs)
438
439 if callable(f):
--> 440 applied = b.apply(f, **kwargs)
441 else:
442 applied = getattr(b, f)(**kwargs)
~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in apply(self, func, **kwargs)
388 """
389 with np.errstate(all="ignore"):
--> 390 result = func(self.values, **kwargs)
391
392 if is_extension_array_dtype(result) and result.ndim > 1:
~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in arithmetic_op(left, right, op, str_rep)
195 else:
196 with np.errstate(all="ignore"):
--> 197 res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep)
198
199 return res_values
~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_arithmetic_op(left, right, op, str_rep)
149 result = expressions.evaluate(op, str_rep, left, right)
150 except TypeError:
--> 151 result = masked_arith_op(left, right, op)
152
153 return missing.dispatch_fill_zeros(op, left, right, result)
~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in masked_arith_op(x, y, op)
110 if mask.any():
111 with np.errstate(all="ignore"):
--> 112 result[mask] = op(xrav[mask], y)
113
114 result, _ = maybe_upcast_putmask(result, ~mask, np.nan)
TypeError: can't multiply sequence by non-int of type 'float'
I am stuck at the area and have searched on the Stack Overflow, youtube about the basic for multiplying in terms of float but, I think my keywords is not on par with the ideas.
I have a table ("trds") that contains fields RefDate, Symbol, Timestamp, Sequence, Quantity, Price, SaleCondition, Pid, and Submkt ("select * from trds limit 100"):
RefDate Symbol Timestamp Sequence Quantity Price SaleCondition Pid SubMkt
1 2015-08-03 AAPL 14400002 507 10 121.50 # TI P
2 2015-08-03 AAPL 14400026 1017 100 121.39 T P
3 2015-08-03 AAPL 14400026 1018 520 121.39 T P
4 2015-08-03 AAPL 14400026 1019 100 121.40 T P
5 2015-08-03 AAPL 14400028 1020 100 121.49 T P
6 2015-08-03 AAPL 14400028 1021 2172 121.49 T P
7 2015-08-03 AAPL 14425890 1026 100 121.49 T P
8 2015-08-03 AAPL 14426989 1027 100 121.49 T P
9 2015-08-03 AAPL 14433658 1028 137 121.40 #FT P
10 2015-08-03 AAPL 14628998 1029 200 121.40 T P
11 2015-08-03 AAPL 14637576 1030 5328 121.40 # T P
12 2015-08-03 AAPL 14637576 1031 200 121.45 #FT Q
13 2015-08-03 AAPL 15116858 1032 3040 121.40 T P
14 2015-08-03 AAPL 15129926 1034 1295 121.40 T P
15 2015-08-03 AAPL 15130128 1035 201 121.30 T P
16 2015-08-03 AAPL 15237959 1036 126 121.30 T P
17 2015-08-03 AAPL 15553155 1043 20 121.03 # TI P
18 2015-08-03 AAPL 15646925 1044 80 121.03 # TI P
19 2015-08-03 AAPL 15844129 1050 40 121.22 #FTI Q
20 2015-08-03 AAPL 16908849 1055 190 121.22 #FT Q
21 2015-08-03 AAPL 16998884 1056 260 121.22 #FT Q
22 2015-08-03 AAPL 17682717 1057 10 121.22 #FTI Q
23 2015-08-03 AAPL 17682717 1058 190 121.23 #FT Q
24 2015-08-03 AAPL 17736566 1059 100 121.23 #FT Q
25 2015-08-03 AAPL 18102786 1061 100 121.22 #FT Q
26 2015-08-03 AAPL 18102786 1062 100 121.22 #FT Q
27 2015-08-03 AAPL 18103386 1063 700 121.23 #FT Q
28 2015-08-03 AAPL 19170799 1064 200 121.22 #FT P
29 2015-08-03 AAPL 19171001 1065 40 121.22 #FTI P
30 2015-08-03 AAPL 19557088 1068 500 121.21 T P
31 2015-08-03 AAPL 19995780 1072 50 121.00 #FTI P
32 2015-08-03 AAPL 19995780 1073 450 121.00 #FT P
33 2015-08-03 AAPL 20110219 1074 100 121.00 #FT P
34 2015-08-03 AAPL 20131347 1075 100 121.00 #FT P
35 2015-08-03 AAPL 20288087 1076 50 121.01 #FTI P
36 2015-08-03 AAPL 20288282 1077 250 121.00 #FT P
37 2015-08-03 AAPL 20776851 1081 200 121.22 T P
38 2015-08-03 AAPL 20777052 1082 300 121.22 T P
39 2015-08-03 AAPL 21416707 1084 200 121.22 T P
40 2015-08-03 AAPL 21416907 1085 300 121.22 T P
41 2015-08-03 AAPL 21740935 1086 200 121.22 #FT P
42 2015-08-03 AAPL 21741136 1087 33 121.22 #FTI P
43 2015-08-03 AAPL 22289290 1088 50 121.22 # TI P
44 2015-08-03 AAPL 22302889 1089 150 121.22 T P
45 2015-08-03 AAPL 22303093 1090 350 121.22 T P
46 2015-08-03 AAPL 22324450 1091 100 121.22 T P
47 2015-08-03 AAPL 22325298 1092 100 121.22 T P
48 2015-08-03 AAPL 22489509 1093 200 121.22 T P
49 2015-08-03 AAPL 22489711 1094 300 121.22 T P
50 2015-08-03 AAPL 22651416 1096 200 121.22 #FT P
51 2015-08-03 AAPL 22656656 1097 77 121.22 # TI P
52 2015-08-03 AAPL 22656857 1098 10 121.23 # TI P
53 2015-08-03 AAPL 22762084 1099 87 121.37 #FTI Q
54 2015-08-03 AAPL 22762084 1100 413 121.38 #FT Q
55 2015-08-03 AAPL 22956526 1103 20 121.20 # TI P
56 2015-08-03 AAPL 22956727 1104 180 121.18 T P
57 2015-08-03 AAPL 23254802 1105 36 121.27 # TI P
58 2015-08-03 AAPL 24077374 1110 144 121.27 T P
59 2015-08-03 AAPL 24077374 1111 50 121.29 # TI P
60 2015-08-03 AAPL 24077374 1112 306 121.30 T P
61 2015-08-03 AAPL 24121832 1113 2 121.30 # TI P
62 2015-08-03 AAPL 24164641 1116 1000 121.30 T P
63 2015-08-03 AAPL 24168414 1117 600 121.30 T P
64 2015-08-03 AAPL 24192881 1120 92 121.30 # TI P
65 2015-08-03 AAPL 24192882 1121 408 121.38 #FT Q
66 2015-08-03 AAPL 24331392 1122 400 121.45 # T P
67 2015-08-03 AAPL 24331392 1123 100 121.38 #FT Q
68 2015-08-03 AAPL 24336664 1124 79 121.38 # TI P
69 2015-08-03 AAPL 24464675 1125 1000 121.40 T P
70 2015-08-03 AAPL 24587817 1126 496 121.38 T P
71 2015-08-03 AAPL 24588021 1127 504 121.39 T P
72 2015-08-03 AAPL 24600057 1128 500 121.38 T P
73 2015-08-03 AAPL 24667891 1130 500 121.37 T P
74 2015-08-03 AAPL 24723357 1131 50 121.39 # TI P
75 2015-08-03 AAPL 24778654 1132 1000 121.39 T P
76 2015-08-03 AAPL 24908008 1133 200 121.39 T P
77 2015-08-03 AAPL 24908210 1134 300 121.40 T P
78 2015-08-03 AAPL 24912914 1135 1500 121.40 T P
79 2015-08-03 AAPL 25011487 1136 500 121.40 T P
80 2015-08-03 AAPL 25018982 1137 100 121.40 T P
81 2015-08-03 AAPL 25023375 1138 21 121.40 # TI P
82 2015-08-03 AAPL 25064872 1139 600 121.40 T P
83 2015-08-03 AAPL 25111247 1140 500 121.40 T P
84 2015-08-03 AAPL 25112575 1141 200 121.40 T P
85 2015-08-03 AAPL 25139881 1142 200 121.40 T P
86 2015-08-03 AAPL 25140659 1143 79 121.40 # TI P
87 2015-08-03 AAPL 25140869 1144 421 121.45 T P
88 2015-08-03 AAPL 25219916 1146 200 121.45 T P
89 2015-08-03 AAPL 25229789 1147 50 121.36 #FTI P
90 2015-08-03 AAPL 25229988 1148 29 121.35 #FTI P
91 2015-08-03 AAPL 25290394 1160 200 121.43 T P
92 2015-08-03 AAPL 25392283 1168 30 121.40 # TI P
93 2015-08-03 AAPL 25421012 1169 300 121.40 T P
94 2015-08-03 AAPL 25755052 1173 2 121.45 # TI P
95 2015-08-03 AAPL 25763189 1174 60 121.44 # TI P
96 2015-08-03 AAPL 25942067 1182 200 121.46 #FT P
97 2015-08-03 AAPL 25942068 1183 100 121.47 #FT P
98 2015-08-03 AAPL 25942068 1184 100 121.47 #FT P
99 2015-08-03 AAPL 25942070 1185 100 121.47 #FT P
100 2015-08-03 AAPL 25948942 1186 1000 121.48 T
I need to create a query that groups together all trades in five minute intervals and finds the Min/Max/Avg and Last Price of each group. "Timestamp" is milliseconds since midnight (so 9:30 is 34200000), and I only want to include between 9:30 and 4:00. I created timebucket by dividing by five minute intervals (300,000 ms): cast(timestamp/300e3) as int) as timebucket.
The end result should look like this:
I can produce code to get the Min/Max/Avg price that looks like this:
dbGetQuery(nqdb, statement = "select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket,
Min(time(t.timestamp/1000, 'unixepoch')) as startTime, Max(t.Price) as MaxPrice,
Min(t.Price) as MinPrice, Avg(t.Price) as AvgPrice
from trds t
where (t.Timestamp between 34200000 and 57600000) and
SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*' group by timeBucket, symbol order by symbol ASC")
This is somewhat close, although the StartTime has seconds in the HH:MM:SS field when it shouldn't, and I've tried everything from sub-queries to sub-selects and searched through "greatest-n-per-group" forums to get the "LastPrice" to no avail. Here's the output:
RefDate Symbol timeBucket startTime MaxPrice MinPrice AvgPrice
1 2015-08-03 AAPL 114 09:30:00 121.5000 121.0500 121.22574
2 2015-08-03 AAPL 115 09:35:00 122.4700 121.2200 121.86515
3 2015-08-03 AAPL 116 09:40:00 122.5700 122.1000 122.37850
4 2015-08-03 AAPL 117 09:45:00 122.2000 121.8901 122.00520
5 2015-08-03 AAPL 118 09:50:00 122.2900 122.0000 122.10803
6 2015-08-03 AAPL 119 09:55:00 122.2100 121.7000 122.02053
7 2015-08-03 AAPL 120 10:00:00 121.8900 121.4100 121.73007
8 2015-08-03 AAPL 121 10:05:00 121.5500 121.2500 121.38513
9 2015-08-03 AAPL 122 10:10:00 121.6200 121.2550 121.48453
10 2015-08-03 AAPL 123 10:15:00 121.5200 121.2700 121.38975
11 2015-08-03 AAPL 124 10:20:00 121.3400 121.1500 121.25174
12 2015-08-03 AAPL 125 10:25:00 121.1600 120.7750 120.92567
13 2015-08-03 AAPL 126 10:30:00 121.0400 120.8100 120.90262
14 2015-08-03 AAPL 127 10:35:01 121.0000 120.7800 120.88182
15 2015-08-03 AAPL 128 10:40:00 121.0000 120.8600 120.91147
16 2015-08-03 AAPL 129 10:45:00 120.9083 120.7330 120.81935
17 2015-08-03 AAPL 130 10:50:00 120.8400 120.5500 120.71769
18 2015-08-03 AAPL 131 10:55:00 120.7200 120.5300 120.62324
19 2015-08-03 AAPL 132 11:00:00 120.8200 120.6500 120.71650
20 2015-08-03 AAPL 133 11:05:00 120.9900 120.6500 120.83935
21 2015-08-03 AAPL 134 11:10:00 121.1800 120.9100 121.02811
22 2015-08-03 AAPL 135 11:15:00 121.1700 120.9700 121.05849
23 2015-08-03 AAPL 136 11:20:01 121.1500 121.0480 121.09072
24 2015-08-03 AAPL 137 11:25:00 121.1500 120.9500 121.04811
25 2015-08-03 AAPL 138 11:30:00 121.1200 120.9200 121.02047
26 2015-08-03 AAPL 139 11:35:00 120.9900 120.6700 120.82496
27 2015-08-03 AAPL 140 11:40:00 120.8400 120.5600 120.68603
28 2015-08-03 AAPL 141 11:45:00 120.8600 120.6600 120.75718
29 2015-08-03 AAPL 142 11:50:00 120.6800 120.3300 120.46856
30 2015-08-03 AAPL 143 11:55:00 120.5155 120.4100 120.45356
31 2015-08-03 AAPL 144 12:00:00 120.5500 120.2200 120.32099
32 2015-08-03 AAPL 145 12:05:00 120.3000 120.1100 120.20832
33 2015-08-03 AAPL 146 12:10:00 120.1734 119.9000 120.02722
34 2015-08-03 AAPL 147 12:15:00 120.0600 119.8700 119.96583
35 2015-08-03 AAPL 148 12:20:00 119.9900 119.8100 119.89728
36 2015-08-03 AAPL 149 12:25:01 120.2200 119.8900 120.03213
37 2015-08-03 AAPL 150 12:30:00 120.1800 119.9200 120.03840
38 2015-08-03 AAPL 151 12:35:00 119.9756 119.7700 119.87204
39 2015-08-03 AAPL 152 12:40:00 119.9000 119.7500 119.82387
40 2015-08-03 AAPL 153 12:45:00 119.8353 118.7000 119.16163
41 2015-08-03 AAPL 154 12:50:00 118.9890 118.2600 118.66432
42 2015-08-03 AAPL 155 12:55:00 118.5000 117.5200 117.95536
43 2015-08-03 AAPL 156 13:00:00 118.4367 117.9500 118.15729
44 2015-08-03 AAPL 157 13:05:00 118.4500 117.7101 118.08337
45 2015-08-03 AAPL 158 13:10:00 118.1200 117.6700 117.87640
46 2015-08-03 AAPL 159 13:15:00 118.0100 117.6601 117.82184
47 2015-08-03 AAPL 160 13:20:00 118.1700 117.6100 117.98232
48 2015-08-03 AAPL 161 13:25:00 118.4400 118.0200 118.27480
49 2015-08-03 AAPL 162 13:30:00 118.4500 118.2000 118.33434
50 2015-08-03 AAPL 163 13:35:00 118.5500 118.3000 118.45115
51 2015-08-03 AAPL 164 13:40:00 118.6400 118.3200 118.50168
52 2015-08-03 AAPL 165 13:45:00 118.6800 118.4400 118.54192
53 2015-08-03 AAPL 166 13:50:00 118.8300 118.6000 118.74204
54 2015-08-03 AAPL 167 13:55:00 118.8400 118.6500 118.73936
55 2015-08-03 AAPL 168 14:00:00 118.7300 118.5200 118.60532
56 2015-08-03 AAPL 169 14:05:00 118.6500 118.4300 118.50543
57 2015-08-03 AAPL 170 14:10:00 118.5800 118.4300 118.49353
58 2015-08-03 AAPL 171 14:15:01 118.7100 118.4600 118.58022
59 2015-08-03 AAPL 172 14:20:00 118.5600 118.4000 118.48883
60 2015-08-03 AAPL 173 14:25:00 118.6500 118.3300 118.49131
61 2015-08-03 AAPL 174 14:30:00 118.6675 118.4900 118.56914
62 2015-08-03 AAPL 175 14:35:00 118.6500 118.4700 118.55887
63 2015-08-03 AAPL 176 14:40:00 118.8100 118.5600 118.69735
64 2015-08-03 AAPL 177 14:45:00 118.8000 118.6200 118.72279
65 2015-08-03 AAPL 178 14:50:00 118.7800 118.6200 118.71055
66 2015-08-03 AAPL 179 14:55:00 118.7500 118.5700 118.65656
67 2015-08-03 AAPL 180 15:00:00 118.8700 118.6500 118.72095
68 2015-08-03 AAPL 181 15:05:00 118.8700 118.6900 118.78130
69 2015-08-03 AAPL 182 15:10:00 118.7200 118.4600 118.61231
70 2015-08-03 AAPL 183 15:15:01 118.6000 118.3000 118.42988
71 2015-08-03 AAPL 184 15:20:03 118.4000 118.0700 118.23301
72 2015-08-03 AAPL 185 15:25:00 118.1000 117.8600 117.99564
73 2015-08-03 AAPL 186 15:30:00 118.1900 117.9600 118.08736
74 2015-08-03 AAPL 187 15:35:00 118.1700 117.8800 117.99449
75 2015-08-03 AAPL 188 15:40:00 118.2700 118.1000 118.16962
76 2015-08-03 AAPL 189 15:45:00 118.2000 117.9600 118.08088
77 2015-08-03 AAPL 190 15:50:00 118.1700 118.0000 118.07813
78 2015-08-03 AAPL 191 15:55:00 118.4500 118.0300 118.23987
79 2015-08-03 AMZN 114 09:30:00 538.5500 536.3200 537.61255
80 2015-08-03 AMZN 115 09:35:04 537.4600 535.7700 536.42177
81 2015-08-03 AMZN 116 09:40:01 538.3500 536.4000 537.68861
82 2015-08-03 AMZN 117 09:45:00 539.3500 536.2800 537.56732
83 2015-08-03 AMZN 118 09:50:00 539.9500 538.7100 539.45455
84 2015-08-03 AMZN 119 09:55:03 540.4400 538.8100 539.66087
85 2015-08-03 AMZN 120 10:00:00 540.2600 537.7100 539.01779
86 2015-08-03 AMZN 121 10:05:00 538.4050 536.5500 537.48823
87 2015-08-03 AMZN 122 10:10:03 537.6800 536.8000 537.21956
88 2015-08-03 AMZN 123 10:15:00 536.8000 534.1500 535.24578
89 2015-08-03 AMZN 124 10:20:01 535.0500 533.7700 534.51831
90 2015-08-03 AMZN 125 10:25:04 535.3900 534.1200 534.74367
91 2015-08-03 AMZN 126 10:30:13 534.9700 534.0110 534.64140
92 2015-08-03 AMZN 127 10:35:04 535.3500 534.5001 534.92485
93 2015-08-03 AMZN 128 10:40:04 535.1600 534.1000 534.57402
94 2015-08-03 AMZN 129 10:45:02 536.0000 534.5100 535.26481
95 2015-08-03 AMZN 130 10:50:00 536.3500 535.4500 535.93280
96 2015-08-03 AMZN 131 10:55:02 536.6500 535.2600 535.96416
97 2015-08-03 AMZN 132 11:00:01 536.6000 535.6800 536.24564
98 2015-08-03 AMZN 133 11:05:10 537.8900 536.4800 537.23168
99 2015-08-03 AMZN 134 11:10:00 538.5200 537.3500 537.94708
100 2015-08-03 AMZN 135 11:15:02 537.9400 537.3500 537.58755
101 2015-08-03 AMZN 136 11:20:05 537.9050 536.8600 537.31598
I'm guessing there's a more elegant solution than the Min(time) work around I used. But the primary question is how to find the LastPrice in each five minute interval?
Update- This is a separate query I did that produces the last price per five minute group, but without Min(Price), Max(Price), and Avg(Price). I do not know how to combine all of them in one query.
dbGetQuery(nqdb, statement = "select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket,
time(t.timestamp/1000, 'unixepoch') as startTime,
t.price as LastPrice from trds t
where (t.Timestamp between 34200000 and 57600000) and
SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*' group by timeBucket, symbol order by symbol ASC")
RefDate Symbol timeBucket startTime LastPrice
1 2015-08-03 AAPL 114 09:34:00 121.2300
2 2015-08-03 AAPL 115 09:39:00 122.4400
3 2015-08-03 AAPL 116 09:44:00 122.1800
4 2015-08-03 AAPL 117 09:49:00 122.0500
5 2015-08-03 AAPL 118 09:54:00 122.0700
6 2015-08-03 AAPL 119 09:59:00 121.7700
7 2015-08-03 AAPL 120 10:04:00 121.5400
8 2015-08-03 AAPL 121 10:09:00 121.2600
9 2015-08-03 AAPL 122 10:14:00 121.5140
10 2015-08-03 AAPL 123 10:19:00 121.2700
11 2015-08-03 AAPL 124 10:24:00 121.1600
12 2015-08-03 AAPL 125 10:29:00 121.0200
13 2015-08-03 AAPL 126 10:34:00 120.9200
14 2015-08-03 AAPL 127 10:39:00 120.9900
15 2015-08-03 AAPL 128 10:44:00 120.8600
16 2015-08-03 AAPL 129 10:49:00 120.7800
17 2015-08-03 AAPL 130 10:54:00 120.6200
18 2015-08-03 AAPL 131 10:59:00 120.6500
19 2015-08-03 AAPL 132 11:04:00 120.6900
20 2015-08-03 AAPL 133 11:09:00 120.9600
21 2015-08-03 AAPL 134 11:14:00 121.0237
22 2015-08-03 AAPL 135 11:19:00 121.0899
23 2015-08-03 AAPL 136 11:24:00 121.1010
24 2015-08-03 AAPL 137 11:29:00 120.9954
25 2015-08-03 AAPL 138 11:34:00 120.9895
26 2015-08-03 AAPL 139 11:39:00 120.6750
27 2015-08-03 AAPL 140 11:44:00 120.8200
28 2015-08-03 AAPL 141 11:49:00 120.6750
29 2015-08-03 AAPL 142 11:54:00 120.4500
30 2015-08-03 AAPL 143 11:59:00 120.4900
31 2015-08-03 AAPL 144 12:04:00 120.2449
32 2015-08-03 AAPL 145 12:09:00 120.1600
33 2015-08-03 AAPL 146 12:14:00 120.0200
34 2015-08-03 AAPL 147 12:19:00 119.8700
35 2015-08-03 AAPL 148 12:24:00 119.9400
36 2015-08-03 AAPL 149 12:29:00 120.1770
37 2015-08-03 AAPL 150 12:34:00 119.9505
38 2015-08-03 AAPL 151 12:39:00 119.8400
39 2015-08-03 AAPL 152 12:44:00 119.8400
40 2015-08-03 AAPL 153 12:49:00 118.9400
41 2015-08-03 AAPL 154 12:54:00 118.4900
42 2015-08-03 AAPL 155 12:59:00 118.0000
43 2015-08-03 AAPL 156 13:04:00 118.4300
44 2015-08-03 AAPL 157 13:09:00 117.7286
45 2015-08-03 AAPL 158 13:14:00 117.9173
46 2015-08-03 AAPL 159 13:19:00 117.6700
47 2015-08-03 AAPL 160 13:24:00 118.1200
48 2015-08-03 AAPL 161 13:29:00 118.3601
49 2015-08-03 AAPL 162 13:34:00 118.4400
50 2015-08-03 AAPL 163 13:39:00 118.3000
Update: per #Parfait-on the timestamp issue, it mostly works, however one of the the output is off with one Symbol. Also still doesn't solve the problem of finding the Last Price per five minute group along with min/max/avg:
157 2015-08-03 DAVE 114 09:30:00 17.9600 17.9300 17.94500
158 2015-08-03 DAVE 115 09:37:00 17.9994 17.9100 17.95470
159 2015-08-03 DAVE 116 09:40:00 17.8700 17.8700 17.87000
160 2015-08-03 DAVE 118 09:50:00 17.7350 17.6950 17.71500
161 2015-08-03 DAVE 120 10:04:00 17.6900 17.6700 17.68000
162 2015-08-03 DAVE 121 10:08:00 17.5600 17.5600 17.56000
163 2015-08-03 DAVE 122 10:14:00 17.5600 17.5600 17.56000
164 2015-08-03 DAVE 124 10:24:00 17.6500 17.6500 17.65000
165 2015-08-03 DAVE 126 10:30:00 17.6000 17.5200 17.56000
166 2015-08-03 DAVE 127 10:39:00 17.4800 17.4800 17.48000
167 2015-08-03 DAVE 128 10:40:00 17.6100 17.4900 17.55000
168 2015-08-03 DAVE 129 10:47:00 17.4400 17.4400 17.44000
169 2015-08-03 DAVE 130 10:54:00 17.5600 17.5600 17.56000
170 2015-08-03 DAVE 131 10:55:00 17.4300 17.3800 17.40500
171 2015-08-03 DAVE 132 11:00:00 17.3400 17.2620 17.30720
172 2015-08-03 DAVE 133 11:09:00 17.2800 17.2700 17.27500
173 2015-08-03 DAVE 135 11:17:00 17.2700 17.2300 17.25000
174 2015-08-03 DAVE 136 11:23:00 17.3200 17.3000 17.31000
175 2015-08-03 DAVE 137 11:25:00 17.4000 17.3750 17.38875
176 2015-08-03 DAVE 139 11:39:00 17.4400 17.4400 17.44000
177 2015-08-03 DAVE 141 11:49:00 17.4200 17.4200 17.42000
178 2015-08-03 DAVE 142 11:51:00 17.4200 17.4200 17.42000
179 2015-08-03 DAVE 143 11:59:00 17.3900 17.3900 17.39000
180 2015-08-03 DAVE 144 12:01:00 17.4350 17.3600 17.39750
181 2015-08-03 DAVE 146 12:13:00 17.3700 17.3600 17.36500
182 2015-08-03 DAVE 147 12:17:00 17.3600 17.3500 17.35333
183 2015-08-03 DAVE 148 12:20:00 17.4000 17.3700 17.38500
184 2015-08-03 DAVE 149 12:25:00 17.3700 17.3700 17.37000
185 2015-08-03 DAVE 150 12:31:00 17.3300 17.3300 17.33000
186 2015-08-03 DAVE 153 12:49:00 17.3800 17.3800 17.38000
187 2015-08-03 DAVE 154 12:50:00 17.3900 17.3900 17.39000
188 2015-08-03 DAVE 155 12:55:00 17.3300 17.3300 17.33000
189 2015-08-03 DAVE 158 13:12:00 17.3400 17.3100 17.32500
190 2015-08-03 DAVE 159 13:15:00 17.3600 17.3400 17.35000
191 2015-08-03 DAVE 160 13:22:00 17.3700 17.3700 17.37000
192 2015-08-03 DAVE 162 13:34:00 17.3504 17.3504 17.35040
193 2015-08-03 DAVE 163 13:35:00 17.3300 17.3100 17.32000
194 2015-08-03 DAVE 165 13:49:00 17.3200 17.3200 17.32000
195 2015-08-03 DAVE 171 14:15:00 17.3500 17.2700 17.31563
196 2015-08-03 DAVE 172 14:20:00 17.4000 17.3500 17.36667
197 2015-08-03 DAVE 175 14:37:00 17.3900 17.3900 17.39000
198 2015-08-03 DAVE 176 14:40:00 17.4000 17.3500 17.37125
199 2015-08-03 DAVE 177 14:45:00 17.4100 17.3500 17.38300
200 2015-08-03 DAVE 178 14:51:00 17.4500 17.4500 17.45000
201 2015-08-03 DAVE 181 15:05:00 17.5800 17.5300 17.55700
202 2015-08-03 DAVE 182 15:10:00 17.5600 17.5200 17.54223
203 2015-08-03 DAVE 184 15:23:00 17.6900 17.6200 17.65500
204 2015-08-03 DAVE 186 15:30:00 17.6900 17.6900 17.69000
205 2015-08-03 DAVE 189 15:46:00 17.7500 17.7200 17.74000
Here's the time in seconds for the problem Symbol "DAVE":
RefDate Symbol timeBucket startTime MaxPrice MinPrice AvgPrice
1 2015-08-03 DAVE 114 34200708 17.9600 17.9300 17.94500
2 2015-08-03 DAVE 115 34628936 17.9994 17.9100 17.95470
3 2015-08-03 DAVE 116 34807939 17.8700 17.8700 17.87000
4 2015-08-03 DAVE 118 35415342 17.7350 17.6950 17.71500
5 2015-08-03 DAVE 120 36250083 17.6900 17.6700 17.68000
6 2015-08-03 DAVE 121 36528404 17.5600 17.5600 17.56000
7 2015-08-03 DAVE 122 36850085 17.5600 17.5600 17.56000
8 2015-08-03 DAVE 124 37450198 17.6500 17.6500 17.65000
9 2015-08-03 DAVE 126 37892189 17.6000 17.5200 17.56000
10 2015-08-03 DAVE 127 38389126 17.4800 17.4800 17.48000
11 2015-08-03 DAVE 128 38400053 17.6100 17.4900 17.55000
12 2015-08-03 DAVE 129 38861146 17.4400 17.4400 17.44000
13 2015-08-03 DAVE 130 39250319 17.5600 17.5600 17.56000
14 2015-08-03 DAVE 131 39301007 17.4300 17.3800 17.40500
15 2015-08-03 DAVE 132 39704588 17.3400 17.2620 17.30720
16 2015-08-03 DAVE 133 40150085 17.2800 17.2700 17.27500
17 2015-08-03 DAVE 135 40668185 17.2700 17.2300 17.25000
18 2015-08-03 DAVE 136 41026622 17.3200 17.3000 17.31000
19 2015-08-03 DAVE 137 41125827 17.4000 17.3750 17.38875
20 2015-08-03 DAVE 139 41950078 17.4400 17.4400 17.44000
21 2015-08-03 DAVE 141 42550193 17.4200 17.4200 17.42000
22 2015-08-03 DAVE 142 42704053 17.4200 17.4200 17.42000
23 2015-08-03 DAVE 143 43150086 17.3900 17.3900 17.39000
24 2015-08-03 DAVE 144 43439310 17.4350 17.3600 17.39750
25 2015-08-03 DAVE 146 44086864 17.3700 17.3600 17.36500
26 2015-08-03 DAVE 147 44250485 17.3600 17.3500 17.35333
27 2015-08-03 DAVE 148 44416744 17.4000 17.3700 17.38500
28 2015-08-03 DAVE 149 44748677 17.3700 17.3700 17.37000
29 2015-08-03 DAVE 150 45087379 17.3300 17.3300 17.33000
30 2015-08-03 DAVE 153 46149940 17.3800 17.3800 17.38000
31 2015-08-03 DAVE 154 46236704 17.3900 17.3900 17.39000
32 2015-08-03 DAVE 155 46513141 17.3300 17.3300 17.33000
33 2015-08-03 DAVE 158 47589401 17.3400 17.3100 17.32500
34 2015-08-03 DAVE 159 47706529 17.3600 17.3400 17.35000
35 2015-08-03 DAVE 160 48134567 17.3700 17.3700 17.37000
36 2015-08-03 DAVE 162 48856648 17.3504 17.3504 17.35040
37 2015-08-03 DAVE 163 48943090 17.3300 17.3100 17.32000
38 2015-08-03 DAVE 165 49755204 17.3200 17.3200 17.32000
39 2015-08-03 DAVE 171 51379248 17.3500 17.2700 17.31563
40 2015-08-03 DAVE 172 51622647 17.4000 17.3500 17.36667
41 2015-08-03 DAVE 175 52636324 17.3900 17.3900 17.39000
42 2015-08-03 DAVE 176 52921459 17.4000 17.3500 17.37125
43 2015-08-03 DAVE 177 53132520 17.4100 17.3500 17.38300
44 2015-08-03 DAVE 178 53516691 17.4500 17.4500 17.45000
45 2015-08-03 DAVE 181 54350810 17.5800 17.5300 17.55700
46 2015-08-03 DAVE 182 54749749 17.5600 17.5200 17.54223
47 2015-08-03 DAVE 184 55476188 17.6900 17.6200 17.65500
48 2015-08-03 DAVE 186 55804762 17.6900 17.6900 17.69000
49 2015-08-03 DAVE 189 56789594 17.7500 17.7200 17.74000
50 2015-08-03 DAVE 190 57274050 17.7800 17.6600 17.73333
51 2015-08-03 DAVE 191 57557752 17.7000 17.6300 17.66265
Consider subtracting its remainder using modulo operator, %, for nearest minute (i.e., 60 seconds):
Min(time(t.timestamp/1000 - (t.timestamp/1000) % 60, 'unixepoch')) as startTime
Also, to find first or last price run multiple aggregations using timestamp then left join the aggregates to a main query. Below uses CTE which should be available with latest SQLite version. Wrap entire query in an R string and it should return the last SELECT resultset as data frame.
Below uses a new five_min_grp and runs a self join of aggregate query but joined on different time stamps with the main unit level query.
with unit as
(select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket,
time(t.timestamp/1000 - (t.timestamp/1000) % 300, 'unixepoch') as five_min_grp,
time(t.timestamp/1000 - (t.timestamp/1000) % 60, 'unixepoch') as startTime,
t.price as Price
from trds t
where (t.Timestamp between 34200000 and 57600000)
and (t.SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*')
),
agg as
(select u.RefDate, u.Symbol, u.five_min_grp,
min(u.startTime) as first_startTime,
max(u.startTime) as last_startTime
from unit u
group by u.RefDate, u.Symbol, u.five_min_grp
),
select u.RefDate, u.Symbol, u.five_min_grp,
avg(u.price) as AvgPrice,
min(u.price) as MinPrice,
max(u.price) as MaxPrice,
min(case when first.first_startTime is not null
then u.price
else null
end) as FirstPrice,
max(case when last.last_startTime is not null
then u.price
else null
end) as LastPrice
from unit u
left join agg first
on first.RefDate = u.RefDate
and first.Symbol = u.Symbol
and first.five_min_grp = u.five_min_grp
and first.first_startTime = u.startTime
left join agg last
on last.RefDate = u.RefDate
and last.Symbol = u.Symbol
and last.five_min_grp = u.five_min_grp
and last.last_startTime = u.startTime
group by u.RefDate, u.Symbol, u.five_min_grp
order by u.symbol
DB Fiddle Demo - does not use CTEs since its SQLite version is dated
I have the following right self-join query performed on oracles HR schema, but I can't really understand what it returns. When I've performed the exactly same query but with LEFT JOIN I understood that it returned all employees regardless they have a supervisor.
The manager ID's are a bit confusing, for example 156, King - but King has ID of 100.
SELECT emps.employee_id as "Employee", emps.last_name, mgr.employee_id as "Manager", mgr.last_name
FROM employees emps
RIGHT JOIN employees mgr
ON emps.manager_id = mgr.employee_id
The result
Employee LAST_NAME Manager LAST_NAME
---------- ------------------------- ---------- -------------------------
101 Kochhar 100 King
102 De Haan 100 King
103 Hunold 102 De Haan
104 Ernst 103 Hunold
105 Austin 103 Hunold
106 Pataballa 103 Hunold
107 Lorentz 103 Hunold
108 Greenberg 101 Kochhar
109 Faviet 108 Greenberg
110 Chen 108 Greenberg
111 Sciarra 108 Greenberg
112 Urman 108 Greenberg
113 Popp 108 Greenberg
114 Raphaely 100 King
115 Khoo 114 Raphaely
116 Baida 114 Raphaely
117 Tobias 114 Raphaely
118 Himuro 114 Raphaely
119 Colmenares 114 Raphaely
120 Weiss 100 King
121 Fripp 100 King
122 Kaufling 100 King
123 Vollman 100 King
124 Mourgos 100 King
125 Nayer 120 Weiss
126 Mikkilineni 120 Weiss
127 Landry 120 Weiss
128 Markle 120 Weiss
129 Bissot 121 Fripp
130 Atkinson 121 Fripp
131 Marlow 121 Fripp
132 Olson 121 Fripp
133 Mallin 122 Kaufling
134 Rogers 122 Kaufling
135 Gee 122 Kaufling
136 Philtanker 122 Kaufling
137 Ladwig 123 Vollman
138 Stiles 123 Vollman
139 Seo 123 Vollman
140 Patel 123 Vollman
141 Rajs 124 Mourgos
142 Davies 124 Mourgos
143 Matos 124 Mourgos
144 Vargas 124 Mourgos
145 Russell 100 King
146 Partners 100 King
147 Errazuriz 100 King
148 Cambrault 100 King
149 Zlotkey 100 King
150 Tucker 145 Russell
151 Bernstein 145 Russell
152 Hall 145 Russell
153 Olsen 145 Russell
154 Cambrault 145 Russell
155 Tuvault 145 Russell
156 King 146 Partners
157 Sully 146 Partners
158 McEwen 146 Partners
159 Smith 146 Partners
160 Doran 146 Partners
161 Sewall 146 Partners
162 Vishney 147 Errazuriz
163 Greene 147 Errazuriz
164 Marvins 147 Errazuriz
165 Lee 147 Errazuriz
166 Ande 147 Errazuriz
167 Banda 147 Errazuriz
168 Ozer 148 Cambrault
169 Bloom 148 Cambrault
170 Fox 148 Cambrault
171 Smith 148 Cambrault
172 Bates 148 Cambrault
173 Kumar 148 Cambrault
174 Abel 149 Zlotkey
175 Hutton 149 Zlotkey
176 Taylor 149 Zlotkey
177 Livingston 149 Zlotkey
178 Grant 149 Zlotkey
179 Johnson 149 Zlotkey
180 Taylor 120 Weiss
181 Fleaur 120 Weiss
182 Sullivan 120 Weiss
183 Geoni 120 Weiss
184 Sarchand 121 Fripp
185 Bull 121 Fripp
186 Dellinger 121 Fripp
187 Cabrio 121 Fripp
188 Chung 122 Kaufling
189 Dilly 122 Kaufling
190 Gates 122 Kaufling
191 Perkins 122 Kaufling
192 Bell 123 Vollman
193 Everett 123 Vollman
194 McCain 123 Vollman
195 Jones 123 Vollman
196 Walsh 124 Mourgos
197 Feeney 124 Mourgos
198 OConnell 124 Mourgos
199 Grant 124 Mourgos
200 Whalen 101 Kochhar
201 Hartstein 100 King
202 Fay 201 Hartstein
203 Mavris 101 Kochhar
204 Baer 101 Kochhar
205 Higgins 101 Kochhar
206 Gietz 205 Higgins
162 Vishney
133 Mallin
136 Philtanker
154 Cambrault
196 Walsh
104 Ernst
184 Sarchand
172 Bates
197 Feeney
150 Tucker
142 Davies
143 Matos
191 Perkins
119 Colmenares
200 Whalen
183 Geoni
180 Taylor
152 Hall
137 Ladwig
139 Seo
126 Mikkilineni
125 Nayer
170 Fox
175 Hutton
129 Bissot
163 Greene
105 Austin
176 Taylor
188 Chung
116 Baida
115 Khoo
144 Vargas
195 Jones
174 Abel
157 Sully
182 Sullivan
156 King
194 McCain
193 Everett
187 Cabrio
117 Tobias
179 Johnson
135 Gee
159 Smith
131 Marlow
190 Gates
169 Bloom
166 Ande
151 Bernstein
204 Baer
203 Mavris
160 Doran
155 Tuvault
107 Lorentz
185 Bull
128 Markle
134 Rogers
140 Patel
168 Ozer
178 Grant
141 Rajs
181 Fleaur
165 Lee
138 Stiles
173 Kumar
206 Gietz
164 Marvins
202 Fay
112 Urman
189 Dilly
110 Chen
153 Olsen
161 Sewall
186 Dellinger
109 Faviet
177 Livingston
198 OConnell
106 Pataballa
111 Sciarra
118 Himuro
132 Olson
192 Bell
113 Popp
171 Smith
127 Landry
167 Banda
130 Atkinson
158 McEwen
199 Grant
195 rows selected
In my opinion, the query with right join is confusing managers and employees. That's why the right join doesn't seem to return a clear answer. With left join, you require "All employees and if there is a related manager, also the manager". With right join, you still get "All employees", no matter if they are managers or not. So the meaning of the "right side" is wrong.
Of course it is the intention of the table to contain both types, but probably you may get a clearer picture by better separation.
Say, a manager is everybody who has no manager_id (that's the case if your table is not a deep tree). Then, look at this modification:
SELECT emps.employee_id as "Employee", emps.last_name, mgr.employee_id as "Manager", mgr.last_name
FROM employees emps
RIGHT JOIN (SELECT * FROM employees WHERE manager_id IS NULL) mgr
ON emps.manager_id = mgr.employee_id
Like this, your data basis for right join would be a proper selection of all managers. They will have showed also an employee, even if they have none. Yet this last "even" does not happen with the kind of relation you have chosen.
Then, I fully agree to #Ameya Desphande that there is a second King with ID 156. Which is even more puzzling ;-)