Oracle HR schema right self join on employees table - sql

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 ;-)

Related

Save file from pixel values in .net core

There is a space-separated string contains 2304(48x48) items. I need simply save this as a 48x48 image file. Downloaded from here
var img = "70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 110 98 91 84 84 90 99 110 126 143 153 158 171 169 172 169 165 129 110 113 107 95 79 66 62 56 57 61 52 43 41 65 61 58 57 56 69 75 70 65 56 54 105 146 154 151 151 155 155 150 147 147 148 152 158 164 172 177 182 186 189 188 190 188 180 167 116 95 103 97 77 72 62 55 58 54 56 52 44 50 43 54 64 63 71 68 64 52 66 119 156 161 164 163 164 167 168 170 174 175 176 178 179 183 187 190 195 197 198 197 198 195 191 190 145 86 100 90 65 57 60 54 51 41 49 56 47 38 44 63 55 46 52 54 55 83 138 157 158 165 168 172 171 173 176 179 179 180 182 185 187 189 189 192 197 200 199 196 198 200 198 197 177 91 87 96 58 58 59 51 42 37 41 47 45 37 35 36 30 41 47 59 94 141 159 161 161 164 170 171 172 176 178 179 182 183 183 187 189 192 192 194 195 200 200 199 199 200 201 197 193 111 71 108 69 55 61 51 42 43 56 54 44 24 29 31 45 61 72 100 136 150 159 163 162 163 170 172 171 174 177 177 180 187 186 187 189 192 192 194 195 196 197 199 200 201 200 197 201 137 58 98 92 57 62 53 47 41 40 51 43 24 35 52 63 75 104 129 143 149 158 162 164 166 171 173 172 174 178 178 179 187 188 188 191 193 194 195 198 199 199 197 198 197 197 197 201 164 52 78 87 69 58 56 50 54 39 44 42 26 31 49 65 91 119 134 145 147 152 159 163 167 171 170 169 174 178 178 179 187 187 185 187 190 188 187 191 197 201 199 199 200 197 196 197 182 58 62 77 61 60 55 49 59 52 54 44 22 30 47 68 102 123 136 144 148 150 153 157 167 172 173 170 171 177 179 178 186 190 186 189 196 193 191 194 190 190 192 197 201 203 199 194 189 69 48 74 56 60 57 50 59 59 51 41 20 34 47 79 111 132 139 143 145 147 150 151 160 169 172 171 167 171 177 177 174 180 182 181 192 196 189 192 198 195 194 196 198 201 202 195 189 70 39 69 61 61 61 53 59 59 45 40 26 40 61 93 124 135 138 142 144 146 151 152 158 165 168 168 165 161 164 173 172 167 172 167 180 198 198 193 199 195 194 198 200 198 197 195 190 65 35 68 59 59 62 57 60 59 50 44 32 54 90 115 132 137 138 140 144 146 146 156 165 168 174 176 176 175 168 168 169 171 175 171 172 192 194 184 198 205 201 194 195 193 195 192 186 57 38 72 65 57 62 58 57 60 54 49 47 79 116 130 138 141 141 139 141 143 145 157 164 164 166 173 174 176 179 179 176 181 189 188 173 180 175 160 182 189 198 192 189 190 190 188 172 46 44 64 66 59 62 57 56 62 53 50 66 103 133 137 141 143 141 136 132 131 136 127 118 111 107 108 123 131 143 154 158 166 177 181 175 170 159 148 171 161 176 185 192 194 188 190 162 53 49 58 63 61 61 55 56 61 51 50 81 116 139 142 142 146 144 136 128 119 112 97 85 90 91 88 92 90 80 81 84 106 122 132 144 145 144 147 163 147 163 173 181 190 187 191 167 61 48 53 61 61 58 54 56 61 51 53 89 123 140 144 145 146 147 136 122 107 99 95 92 90 87 83 76 67 52 46 52 63 69 83 96 119 132 148 159 136 137 143 138 143 152 156 156 70 48 50 59 61 57 54 54 61 52 56 93 124 135 140 144 148 150 140 125 114 101 80 54 56 54 41 41 33 40 39 35 49 60 63 74 107 129 147 147 116 111 100 77 76 86 108 111 73 49 50 60 62 60 57 55 63 59 56 89 121 134 139 146 151 152 150 141 127 111 96 77 85 70 32 31 37 91 65 50 48 59 73 83 112 136 155 130 60 46 38 40 43 81 116 91 72 52 48 58 62 62 59 53 61 59 52 85 114 134 140 147 154 159 158 153 145 143 150 126 121 125 68 45 89 137 95 70 78 75 95 109 131 153 171 94 23 16 32 82 82 65 113 77 71 54 48 56 62 62 60 53 60 56 52 75 108 133 141 149 158 166 169 167 163 156 155 146 112 119 134 127 142 140 121 117 129 114 120 129 146 174 191 98 46 33 33 109 147 98 109 67 73 55 50 56 64 64 61 58 61 53 54 64 106 129 140 148 159 169 175 176 174 165 159 156 145 120 115 124 127 131 133 141 147 142 141 147 161 182 202 154 114 96 100 158 158 153 123 61 76 57 48 56 64 64 63 62 61 54 55 44 97 131 137 147 158 168 177 181 183 179 170 168 169 165 155 152 151 152 154 162 165 158 153 158 168 187 206 186 147 135 144 145 152 178 115 57 74 58 48 58 64 63 63 59 63 55 53 66 104 130 132 144 153 162 170 180 185 187 181 178 182 180 177 173 171 171 177 176 172 164 161 167 164 185 207 197 173 152 141 141 161 191 104 54 69 60 48 57 65 62 60 57 64 55 50 94 111 124 130 135 150 159 163 172 179 184 184 178 178 177 173 171 174 177 178 176 169 165 161 163 161 180 205 201 183 171 177 178 180 194 101 55 65 60 47 55 65 63 59 58 63 57 52 90 105 117 122 130 143 153 157 163 171 174 182 183 182 178 174 175 175 177 175 172 163 161 159 157 162 178 200 201 188 181 172 177 187 198 98 57 63 61 48 52 61 64 63 60 65 57 51 95 104 113 117 127 136 145 152 156 162 162 165 173 177 182 183 183 180 181 177 165 153 154 152 153 160 174 193 200 188 185 180 182 192 196 101 60 60 56 49 50 60 66 64 62 64 59 53 99 104 111 112 118 132 142 147 155 158 160 159 162 171 176 184 186 183 180 169 154 141 135 145 155 164 180 196 205 188 189 188 189 193 192 98 61 64 55 49 49 60 66 63 64 63 60 57 99 105 108 112 113 125 139 143 150 155 158 164 169 174 176 182 183 182 177 163 141 133 147 151 164 170 185 200 210 194 188 192 186 185 180 88 64 67 60 46 50 59 65 64 64 64 59 56 101 103 108 109 109 118 134 143 143 147 155 159 166 171 174 177 179 178 172 153 129 143 161 159 166 171 186 197 207 203 185 191 183 179 164 73 67 67 66 48 50 57 65 65 63 64 61 57 103 108 114 112 110 115 128 138 144 145 152 156 159 164 168 172 172 169 161 139 125 147 156 161 162 164 180 188 188 197 185 187 181 180 137 65 70 68 70 52 47 53 62 65 63 65 61 58 105 109 112 120 113 112 122 134 141 149 150 153 155 159 164 167 167 162 152 134 115 126 119 106 99 109 141 158 150 155 175 184 176 175 106 63 70 68 68 50 46 50 57 63 63 64 61 59 107 110 110 117 117 114 117 128 137 147 148 150 153 156 161 162 163 156 150 148 105 70 45 26 25 47 73 74 79 128 177 180 173 157 77 66 68 67 68 52 49 51 56 62 62 62 62 60 101 107 108 114 115 114 117 125 134 143 148 149 152 154 158 160 158 155 160 158 132 88 73 73 64 52 66 91 138 160 174 173 171 125 64 67 63 64 68 54 50 49 54 60 60 60 62 60 98 105 105 109 111 114 117 125 131 139 145 148 153 153 156 157 156 161 168 165 153 139 122 115 105 89 103 150 182 161 171 173 162 89 64 64 62 64 69 56 48 49 56 58 60 59 62 60 89 99 108 106 109 111 119 120 125 134 140 146 152 153 153 153 156 159 162 160 150 136 129 133 133 122 133 148 178 168 168 175 132 61 67 66 65 63 69 57 47 50 55 58 59 61 62 60 89 96 105 107 105 107 117 120 123 124 133 141 149 153 151 145 151 145 139 140 138 128 126 124 129 125 136 142 164 172 168 168 87 58 67 63 62 61 69 57 39 44 55 56 59 63 62 62 84 91 92 98 102 103 113 119 121 118 128 138 146 151 147 142 140 128 127 128 129 126 135 140 135 130 143 146 149 166 174 131 62 65 62 59 67 63 68 83 89 65 42 52 60 60 62 63 77 84 84 91 99 101 107 112 117 118 122 134 145 149 144 134 127 127 129 130 134 125 126 132 152 153 151 150 151 165 171 87 59 65 64 61 58 86 122 138 208 207 154 71 52 56 55 56 69 77 83 85 93 91 102 112 116 118 119 127 140 144 142 131 112 95 85 75 62 58 56 59 87 88 83 127 142 165 149 62 65 62 59 77 113 192 156 84 185 196 197 168 81 70 75 69 58 65 73 82 81 79 95 107 114 116 116 123 136 142 136 132 131 102 71 58 49 41 33 41 36 49 60 99 136 168 111 53 63 71 138 186 203 195 146 87 91 72 79 95 103 82 61 74 55 57 68 75 76 77 84 96 106 110 111 121 130 138 136 142 153 159 152 152 154 145 133 136 147 158 156 155 147 158 74 57 60 123 181 174 126 89 72 67 57 43 55 67 76 86 60 45 51 45 52 68 75 73 77 88 96 100 104 113 115 121 134 146 149 146 149 148 155 168 174 179 178 169 169 174 161 131 44 47 82 150 168 136 104 75 66 80 67 58 48 54 68 88 121 102 51 45 38 53 66 65 70 86 92 96 102 103 109 116 130 136 136 133 136 138 137 135 128 130 143 158 165 164 147 87 62 74 123 160 170 100 99 107 79 71 86 75 57 45 49 65 122 130 43 48 40 39 55 61 59 71 82 87 88 93 105 118 123 128 130 124 111 98 94 88 67 55 84 129 147 148 105 48 82 142 161 164 164 76 72 85 100 88 72 90 84 54 48 54 73 100 73 36 44 31 37 53 51 55 67 74 77 87 97 108 118 125 132 122 106 86 80 82 75 73 83 110 129 126 46 22 130 177 196 193 166 72 52 54 73 100 92 75 99 95 65 68 61 63 91 65 42 37 22 28 39 44 57 68 74 83 92 101 119 131 143 141 134 136 140 139 134 136 139 138 136 85 23 114 202 198 199 180 173 98 36 86 130 150 137 99 77 101 99 72 56 43 77 82 79 70 56 28 20 25 36 50 63 73 83 98 111 124 139 156 160 159 169 168 165 163 159 149 114 43 26 133 183 192 177 152 137 130 125 139 173 195 186 137 101 88 101 105 70 46 77 72 84 87 87 81 64 37 20 31 40 46 65 88 108 110 125 149 157 153 162 164 158 159 154 140 78 21 11 61 144 168 173 157 138 150 148 132 159 182 183 136 106 116 95 106 109 82";
//save string as byte array
var arrrStr = img.Split(" ").Select(s => Convert.ToString(s)).ToArray();
var byt = arrrStr.Select(byte.Parse).ToArray();
//save the file by this array, the line below throws an exception.
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(byt)))
{
image.Save("output.jpg", ImageFormat.Jpeg); // Or Png
}
And as you guess it doesn't work, how to convert this pixel string to file(this value is generated from a file in origin)

Finding Last Record Per Group with Multiple Aggregate Functions- SQLite

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

Why the natural join's result rows count smaller than join's?

The database data is from http://www.w3resource.com/mysql-exercises/join-exercises/
sqlite> select * from employees;
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- ------------- ------------- ---------- -------------------- ------------ ------------ ---------- -------------- ---------- -------------
100 Steven King SKING 515.123.4567 1987-06-17 AD_PRES 24000 0.0 0 90
101 Neena Kochhar NKOCHHAR 515.123.4568 1987-06-18 AD_VP 17000 0.0 100 90
102 Lex De Haan LDEHAAN 515.123.4569 1987-06-19 AD_VP 17000 0.0 100 90
...
202 Pat Fay PFAY 603.123.6666 1987-09-27 MK_REP 6000 0.0 201 20
203 Susan Mavris SMAVRIS 515.123.7777 1987-09-28 HR_REP 6500 0.0 101 40
204 Hermann Baer HBAER 515.123.8888 1987-09-29 PR_REP 10000 0.0 101 70
205 Shelley Higgins SHIGGINS 515.123.8080 1987-09-30 AC_MGR 12000 0.0 101 110
206 William Gietz WGIETZ 515.123.8181 1987-10-01 AC_ACCOUNT 8300 0.0 205 110
sqlite> select * from departments;
DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
------------- ---------------------- ---------- -----------
10 Administration 200 1700
20 Marketing 201 1800
30 Purchasing 114 1700
40 Human Resources 203 2400
50 Shipping 121 1500
60 IT 103 1400
70 Public Relations 204 2700
80 Sales 145 2500
90 Executive 100 1700
100 Finance 108 1700
110 Accounting 205 1700
120 Treasury 0 1700
130 Corporate Tax 0 1700
140 Control And Credit 0 1700
150 Shareholder Services 0 1700
160 Benefits 0 1700
170 Manufacturing 0 1700
180 Construction 0 1700
190 Contracting 0 1700
200 Operations 0 1700
210 IT Support 0 1700
220 NOC 0 1700
230 IT Helpdesk 0 1700
240 Government Sales 0 1700
250 Retail Sales 0 1700
260 Recruiting 0 1700
270 Payroll 0 1700
The natural join result:
sqlite> select * from employees e natural join departments d;
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID DEPARTMENT_NAME LOCATION_ID
----------- ------------- ------------- ---------- -------------------- ------------ ------------ ---------- -------------- ---------- ------------- ---------------------- -----------
101 Neena Kochhar NKOCHHAR 515.123.4568 1987-06-18 AD_VP 17000 0.0 100 90 Executive 1700
102 Lex De Haan LDEHAAN 515.123.4569 1987-06-19 AD_VP 17000 0.0 100 90 Executive 1700
104 Bruce Ernst BERNST 590.423.4568 1987-06-21 IT_PROG 6000 0.0 103 60 IT 1400
105 David Austin DAUSTIN 590.423.4569 1987-06-22 IT_PROG 4800 0.0 103 60 IT 1400
106 Valli Pataballa VPATABAL 590.423.4560 1987-06-23 IT_PROG 4800 0.0 103 60 IT 1400
107 Diana Lorentz DLORENTZ 590.423.5567 1987-06-24 IT_PROG 4200 0.0 103 60 IT 1400
109 Daniel Faviet DFAVIET 515.124.4169 1987-06-26 FI_ACCOUNT 9000 0.0 108 100 Finance 1700
110 John Chen JCHEN 515.124.4269 1987-06-27 FI_ACCOUNT 8200 0.0 108 100 Finance 1700
111 Ismael Sciarra ISCIARRA 515.124.4369 1987-06-28 FI_ACCOUNT 7700 0.0 108 100 Finance 1700
112 Jose Manuel Urman JMURMAN 515.124.4469 1987-06-29 FI_ACCOUNT 7800 0.0 108 100 Finance 1700
113 Luis Popp LPOPP 515.124.4567 1987-06-30 FI_ACCOUNT 6900 0.0 108 100 Finance 1700
115 Alexander Khoo AKHOO 515.127.4562 1987-07-02 PU_CLERK 3100 0.0 114 30 Purchasing 1700
116 Shelli Baida SBAIDA 515.127.4563 1987-07-03 PU_CLERK 2900 0.0 114 30 Purchasing 1700
117 Sigal Tobias STOBIAS 515.127.4564 1987-07-04 PU_CLERK 2800 0.0 114 30 Purchasing 1700
118 Guy Himuro GHIMURO 515.127.4565 1987-07-05 PU_CLERK 2600 0.0 114 30 Purchasing 1700
119 Karen Colmenares KCOLMENA 515.127.4566 1987-07-06 PU_CLERK 2500 0.0 114 30 Purchasing 1700
129 Laura Bissot LBISSOT 650.124.5234 1987-07-16 ST_CLERK 3300 0.0 121 50 Shipping 1500
130 Mozhe Atkinson MATKINSO 650.124.6234 1987-07-17 ST_CLERK 2800 0.0 121 50 Shipping 1500
131 James Marlow JAMRLOW 650.124.7234 1987-07-18 ST_CLERK 2500 0.0 121 50 Shipping 1500
132 TJ Olson TJOLSON 650.124.8234 1987-07-19 ST_CLERK 2100 0.0 121 50 Shipping 1500
150 Peter Tucker PTUCKER 011.44.1344.129268 1987-08-06 SA_REP 10000 0.3 145 80 Sales 2500
151 David Bernstein DBERNSTE 011.44.1344.345268 1987-08-07 SA_REP 9500 0.25 145 80 Sales 2500
152 Peter Hall PHALL 011.44.1344.478968 1987-08-08 SA_REP 9000 0.25 145 80 Sales 2500
153 Christopher Olsen COLSEN 011.44.1344.498718 1987-08-09 SA_REP 8000 0.2 145 80 Sales 2500
154 Nanette Cambrault NCAMBRAU 011.44.1344.987668 1987-08-10 SA_REP 7500 0.2 145 80 Sales 2500
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 1987-08-11 SA_REP 7000 0.15 145 80 Sales 2500
184 Nandita Sarchand NSARCHAN 650.509.1876 1987-09-09 SH_CLERK 4200 0.0 121 50 Shipping 1500
185 Alexis Bull ABULL 650.509.2876 1987-09-10 SH_CLERK 4100 0.0 121 50 Shipping 1500
186 Julia Dellinger JDELLING 650.509.3876 1987-09-11 SH_CLERK 3400 0.0 121 50 Shipping 1500
187 Anthony Cabrio ACABRIO 650.509.4876 1987-09-12 SH_CLERK 3000 0.0 121 50 Shipping 1500
202 Pat Fay PFAY 603.123.6666 1987-09-27 MK_REP 6000 0.0 201 20 Marketing 1800
206 William Gietz WGIETZ 515.123.8181 1987-10-01 AC_ACCOUNT 8300 0.0 205 110 Accounting 1700
sqlite> select count(*) from employees e natural join departments d;
count(*)
----------
32
The join result:
sqlite> select * from employees e join departments d using
(department_id);
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
----------- ------------- ------------- ---------- -------------------- ------------ ------------ ---------- -------------- ---------- ------------- ---------------------- ---------- -----------
100 Steven King SKING 515.123.4567 1987-06-17 AD_PRES 24000 0.0 0 90 Executive 100 1700
101 Neena Kochhar NKOCHHAR 515.123.4568 1987-06-18 AD_VP 17000 0.0 100 90 Executive 100 1700
102 Lex De Haan LDEHAAN 515.123.4569 1987-06-19 AD_VP 17000 0.0 100 90 Executive 100 1700
103 Alexander Hunold AHUNOLD 590.423.4567 1987-06-20 IT_PROG 9000 0.0 102 60 IT 103 1400
104 Bruce Ernst BERNST 590.423.4568 1987-06-21 IT_PROG 6000 0.0 103 60 IT 103 1400
105 David Austin DAUSTIN 590.423.4569 1987-06-22 IT_PROG 4800 0.0 103 60 IT 103 1400
106 Valli Pataballa VPATABAL 590.423.4560 1987-06-23 IT_PROG 4800 0.0 103 60 IT 103 1400
107 Diana Lorentz DLORENTZ 590.423.5567 1987-06-24 IT_PROG 4200 0.0 103 60 IT 103 1400
108 Nancy Greenberg NGREENBE 515.124.4569 1987-06-25 FI_MGR 12000 0.0 101 100 Finance 108 1700
109 Daniel Faviet DFAVIET 515.124.4169 1987-06-26 FI_ACCOUNT 9000 0.0 108 100 Finance 108 1700
110 John Chen JCHEN 515.124.4269 1987-06-27 FI_ACCOUNT 8200 0.0 108 100 Finance 108 1700
111 Ismael Sciarra ISCIARRA 515.124.4369 1987-06-28 FI_ACCOUNT 7700 0.0 108 100 Finance 108 1700
112 Jose Manuel Urman JMURMAN 515.124.4469 1987-06-29 FI_ACCOUNT 7800 0.0 108 100 Finance 108 1700
...
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 1987-08-11 SA_REP 7000 0.15 145 80 Sales 145 2500
156 Janette King JKING 011.44.1345.429268 1987-08-12 SA_REP 10000 0.35 146 80 Sales 145 2500
157 Patrick Sully PSULLY 011.44.1345.929268 1987-08-13 SA_REP 9500 0.35 146 80 Sales 145 2500
158 Allan McEwen AMCEWEN 011.44.1345.829268 1987-08-14 SA_REP 9000 0.35 146 80 Sales 145 2500
159 Lindsey Smith LSMITH 011.44.1345.729268 1987-08-15 SA_REP 8000 0.3 146 80 Sales 145 2500
160 Louise Doran LDORAN 011.44.1345.629268 1987-08-16 SA_REP 7500 0.3 146 80 Sales 145 2500
161 Sarath Sewall SSEWALL 011.44.1345.529268 1987-08-17 SA_REP 7000 0.25 146 80 Sales 145 2500
162 Clara Vishney CVISHNEY 011.44.1346.129268 1987-08-18 SA_REP 10500 0.25 147 80 Sales 145 2500
163 Danielle Greene DGREENE 011.44.1346.229268 1987-08-19 SA_REP 9500 0.15 147 80 Sales 145 2500
164 Mattea Marvins MMARVINS 011.44.1346.329268 1987-08-20 SA_REP 7200 0.1 147 80 Sales 145 2500
165 David Lee DLEE 011.44.1346.529268 1987-08-21 SA_REP 6800 0.1 147 80 Sales 145 2500
166 Sundar Ande SANDE 011.44.1346.629268 1987-08-22 SA_REP 6400 0.1 147 80 Sales 145 2500
167 Amit Banda ABANDA 011.44.1346.729268 1987-08-23 SA_REP 6200 0.1 147 80 Sales 145 2500
168 Lisa Ozer LOZER 011.44.1343.929268 1987-08-24 SA_REP 11500 0.25 148 80 Sales 145 2500
169 Harrison Bloom HBLOOM 011.44.1343.829268 1987-08-25 SA_REP 10000 0.2 148 80 Sales 145 2500
170 Tayler Fox TFOX 011.44.1343.729268 1987-08-26 SA_REP 9600 0.2 148 80 Sales 145 2500
171 William Smith WSMITH 011.44.1343.629268 1987-08-27 SA_REP 7400 0.15 148 80 Sales 145 2500
172 Elizabeth Bates EBATES 011.44.1343.529268 1987-08-28 SA_REP 7300 0.15 148 80 Sales 145 2500
173 Sundita Kumar SKUMAR 011.44.1343.329268 1987-08-29 SA_REP 6100 0.1 148 80 Sales 145 2500
174 Ellen Abel EABEL 011.44.1644.429267 1987-08-30 SA_REP 11000 0.3 149 80 Sales 145 2500
175 Alyssa Hutton AHUTTON 011.44.1644.429266 1987-08-31 SA_REP 8800 0.25 149 80 Sales 145 2500
176 Jonathon Taylor JTAYLOR 011.44.1644.429265 1987-09-01 SA_REP 8600 0.2 149 80 Sales 145 2500
177 Jack Livingston JLIVINGS 011.44.1644.429264 1987-09-02 SA_REP 8400 0.2 149 80 Sales 145 2500
179 Charles Johnson CJOHNSON 011.44.1644.429262 1987-09-04 SA_REP 6200 0.1 149 80 Sales 145 2500
180 Winston Taylor WTAYLOR 650.507.9876 1987-09-05 SH_CLERK 3200 0.0 120 50 Shipping 121 1500
181 Jean Fleaur JFLEAUR 650.507.9877 1987-09-06 SH_CLERK 3100 0.0 120 50 Shipping 121 1500
182 Martha Sullivan MSULLIVA 650.507.9878 1987-09-07 SH_CLERK 2500 0.0 120 50 Shipping 121 1500
183 Girard Geoni GGEONI 650.507.9879 1987-09-08 SH_CLERK 2800 0.0 120 50 Shipping 121 1500
184 Nandita Sarchand NSARCHAN 650.509.1876 1987-09-09 SH_CLERK 4200 0.0 121 50 Shipping 121 1500
185 Alexis Bull ABULL 650.509.2876 1987-09-10 SH_CLERK 4100 0.0 121 50 Shipping 121 1500
186 Julia Dellinger JDELLING 650.509.3876 1987-09-11 SH_CLERK 3400 0.0 121 50 Shipping 121 1500
187 Anthony Cabrio ACABRIO 650.509.4876 1987-09-12 SH_CLERK 3000 0.0 121 50 Shipping 121 1500
188 Kelly Chung KCHUNG 650.505.1876 1987-09-13 SH_CLERK 3800 0.0 122 50 Shipping 121 1500
189 Jennifer Dilly JDILLY 650.505.2876 1987-09-14 SH_CLERK 3600 0.0 122 50 Shipping 121 1500
190 Timothy Gates TGATES 650.505.3876 1987-09-15 SH_CLERK 2900 0.0 122 50 Shipping 121 1500
191 Randall Perkins RPERKINS 650.505.4876 1987-09-16 SH_CLERK 2500 0.0 122 50 Shipping 121 1500
192 Sarah Bell SBELL 650.501.1876 1987-09-17 SH_CLERK 4000 0.0 123 50 Shipping 121 1500
193 Britney Everett BEVERETT 650.501.2876 1987-09-18 SH_CLERK 3900 0.0 123 50 Shipping 121 1500
194 Samuel McCain SMCCAIN 650.501.3876 1987-09-19 SH_CLERK 3200 0.0 123 50 Shipping 121 1500
195 Vance Jones VJONES 650.501.4876 1987-09-20 SH_CLERK 2800 0.0 123 50 Shipping 121 1500
196 Alana Walsh AWALSH 650.507.9811 1987-09-21 SH_CLERK 3100 0.0 124 50 Shipping 121 1500
197 Kevin Feeney KFEENEY 650.507.9822 1987-09-22 SH_CLERK 3000 0.0 124 50 Shipping 121 1500
198 Donald OConnell DOCONNEL 650.507.9833 1987-09-23 SH_CLERK 2600 0.0 124 50 Shipping 121 1500
199 Douglas Grant DGRANT 650.507.9844 1987-09-24 SH_CLERK 2600 0.0 124 50 Shipping 121 1500
200 Jennifer Whalen JWHALEN 515.123.4444 1987-09-25 AD_ASST 4400 0.0 101 10 Administration 200 1700
201 Michael Hartstein MHARTSTE 515.123.5555 1987-09-26 MK_MAN 13000 0.0 100 20 Marketing 201 1800
202 Pat Fay PFAY 603.123.6666 1987-09-27 MK_REP 6000 0.0 201 20 Marketing 201 1800
203 Susan Mavris SMAVRIS 515.123.7777 1987-09-28 HR_REP 6500 0.0 101 40 Human Resources 203 2400
204 Hermann Baer HBAER 515.123.8888 1987-09-29 PR_REP 10000 0.0 101 70 Public Relations 204 2700
205 Shelley Higgins SHIGGINS 515.123.8080 1987-09-30 AC_MGR 12000 0.0 101 110 Accounting 205 1700
206 William Gietz WGIETZ 515.123.8181 1987-10-01 AC_ACCOUNT 8300 0.0 205 110 Accounting 205 1700
sqlite> select count(*) from employees e join departments d using (department_id);
count(*)
----------
106
The natural join result rows count should be same as join, but not, why?
The different between a natural join and a 'normal' join is that the former use all columns that happen to have the same name in both tables.
In this case, both DEPARTMENT_ID and MANAGER_ID match, so the natural join is actually the same as this query:
select * from employees e join departments d using (department_id, manager_id);
This is why you should never, ever use a natural join.

Pandas series bar chart plotting

I have the following data in the format of pandas.core.series.Series (after processing the original DataFrame), and I want to do some visualisation of the data. What I need is to plot a bar chart per Fruit (Apple, Pear, Oranges, etc.) where the annual production of producers X1, X2, X3 are next to each other on the chart (multiple bar chart). The x axis of the figure should be the Year.
Could anyone help please!
Thanks
The data:
Fruit Producer Year Production (tons)
Apple X1 1981 125
1982 146
1983 251
1984 278
1985 161
X2 1981 510
1982 456
1983 531
1984 563
1985 508
X3 1981 68
1982 121
1983 126
1984 189
1985 134
Pear X1 1981 126
1982 148
1983 255
1984 272
1985 166
X2 1981 515
1982 454
1983 539
1984 565
1985 558
X3 1981 516
1982 485
1983 567
1984 519
1985 588
Oranges X1 1981 68
1982 100
1983 109
1984 190
1985 136
X2 1981 50
1982 155
1983 126
1984 155
1985 139
X3 1981 12
1982 163
1983 198
1984 174
1985 136

How to encode a text file using ASMO449+? .NET

Dear All, How I can encode a text file to ASMO449+?
Thanks
That's code page 709. Difficult, .NET doesn't support it. Best thing to do is to using code page 1256, the Windows code page for Arabic, then translate the bytes using this conversion table (also available as a webpage):
/*000-015*/ 000 001 249 003 004 005 006 007 008 009 010 011 012 013 014 015
/*016-031*/ 016 017 018 019 022 023 024 025 026 027 028 029 030 031 254 255
/*032-047*/ 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047
/*048-063*/ 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063
/*064-079*/ 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079
/*080-095*/ 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095
/*096-111*/ 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111
/*112-127*/ 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/*128-143*/ 128 132 174 159 134 141 142 143 144 145 146 190 148 149 155 156
/*144-159*/ 157 158 160 161 162 002 163 224 164 165 166 188 167 252 168 169
/*160-175*/ 171 172 154 176 177 178 179 021 180 181 182 183 184 173 185 186
/*176-191*/ 189 192 220 221 222 223 020 250 153 243 187 244 245 246 247 191
/*192-207*/ 248 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/*208-223*/ 208 209 210 211 212 213 214 170 215 216 217 218 219 225 226 227
/*224-239*/ 133 228 131 229 230 231 232 135 138 130 136 137 233 234 140 139
/*240-255*/ 235 236 237 238 147 239 240 175 241 151 242 150 129 251 152 253
var enc = Encoding.GetEncoding(1256);
var ara = "العربية";
var res = enc.GetBytes(ara);
// TODO: apply table
//...