Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rounding and rename 'amt' to 'amount' wherever necessary #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 33 additions & 34 deletions docs/modules/ROOT/pages/sql_stock_ticker_cloud.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,22 @@ Because the data is randomly generated, you may have to wait several seconds for

+
```sql
SELECT ticker AS Symbol, ROUND(price,2) AS Price, amt AS "Shares Sold"
SELECT ticker AS Symbol, ROUND(price,2) AS Price, amount AS "Shares Sold"
FROM trades_topic;
```
. Limit the output to one stock symbol.
+
```sql
SELECT ticker AS Symbol, ROUND(price,2) AS Price, amt AS "Shares Sold"
SELECT ticker AS Symbol, ROUND(price,2) AS Price, amount AS "Shares Sold"
FROM trades_topic
WHERE ticker = 'APPL';
```
. Limit the output to one symbol and sales of over 50 shares.
+
```sql
SELECT ticker AS Symbol, ROUND(price,2), amt AS "Shares Sold"
SELECT ticker AS Symbol, ROUND(price,2) AS Price, amount AS "Shares Sold"
FROM trades_topic
WHERE ticker = 'VOO' AND amt > 50;
WHERE ticker = 'VOO' AND amount > 50;
```

== Step 3. Enriching the Data
Expand Down Expand Up @@ -183,11 +183,11 @@ SELECT * FROM companies;
. Use a JOIN to combine the static company information with the streaming data.
+
```sql
SELECT
trades.ticker AS Symbol,
companies.company as Company,
ROUND(trades.price,2) AS Price,
trades.amt AS "Shares Sold"
SELECT
trades.ticker AS Symbol,
companies.company as Company,
ROUND(trades.price,2) AS Price,
trades.amount AS "Shares Sold"
FROM trades_topic AS trades
JOIN companies
ON companies.ticker = trades.ticker;
Expand Down Expand Up @@ -217,19 +217,18 @@ SELECT *
. For our first aggregation, we'll display the minimum and maximum price for each stock over a 5 second window. (Output will not appear until 5 seconds have elapsed.)
+
```sql
SELECT
SELECT
window_start,
window_end,
ticker,
ROUND(MAX(price),2) AS high,
window_end,
ticker,
ROUND(MAX(price),2) AS high,
ROUND(MIN(price),2) AS low
FROM TABLE(TUMBLE(
TABLE trades_ordered,
DESCRIPTOR(trade_ts),
INTERVAL '5' SECONDS
))
GROUP BY 1,2,3
;
GROUP BY 1,2,3;
```
This query will display the average price over a 5 second window, updating the result every second.
+
Expand Down Expand Up @@ -276,15 +275,15 @@ CREATE OR REPLACE VIEW high_low AS
. Join the ```trades_ordered``` stream and the ```high_low``` stream to display ticker symbol, high, low, and current price.
+
```sql
SELECT
SELECT
tro.ticker AS Symbol,
tro.price AS Price,
ROUND(tro.price,2) AS Price,
hl.high AS High,
hl.low AS Low
FROM trades_ordered AS tro
JOIN high_low AS hl
ON tro.ticker = hl.ticker
AND hl.window_end BETWEEN tro.trade_ts AND tro.trade_ts + INTERVAL '0.1' SECONDS;
ON tro.ticker = hl.ticker
AND hl.window_end BETWEEN tro.trade_ts AND tro.trade_ts + INTERVAL '0.1' SECONDS;
```
. Create a view for the average price display above. This creates another new data stream.
+
Expand Down Expand Up @@ -319,9 +318,9 @@ AND pr.window_end BETWEEN tro.trade_ts AND tro.trade_ts + INTERVAL '0.1' SECOND;
. Add a column that displays whether the stock value is up or down from the previous average.
+
```sql
SELECT
tro.ticker AS Symbol,
tro.price AS Price,
SELECT
tro.ticker AS Symbol,
ROUND(tro.price,2) AS Price,
pr.average AS Average,
ROUND(((tro.price/pr.average)-1)*100,2) AS Percent_Change,
CASE
Expand All @@ -331,7 +330,7 @@ SELECT
FROM trades_ordered AS tro
JOIN priceavg AS pr
ON tro.ticker = pr.ticker
AND pr.window_end BETWEEN tro.trade_ts AND tro.trade_ts + INTERVAL '0.1' SECOND;
AND pr.window_end BETWEEN tro.trade_ts AND tro.trade_ts + INTERVAL '0.1' SECOND;
```

== Step 6: Create an SQL Job
Expand All @@ -343,12 +342,12 @@ Our job will populate a table in memory. To keep memory use at a minimum, the ou
. Create a view that enriches `trades_ordered` view from Step 4 with the data in the `companies` IMap from Step 3.
+
``` sql
CREATE OR REPLACE VIEW tro_enriched AS
SELECT
tro.ticker AS ticker,
CREATE OR REPLACE VIEW tro_enriched AS
SELECT
tro.ticker AS ticker,
companies.company as company,
ROUND(tro.price,2) AS price,
tro.amt AS shares,
ROUND(tro.price,2) AS price,
tro.amount AS shares,
tro.trade_ts
FROM trades_ordered AS tro
JOIN companies
Expand Down Expand Up @@ -380,7 +379,7 @@ SELECT * FROM high_low_enriched;
+
```sql
SELECT * FROM high_low_enriched
WHERE ticker = 'APPL';
WHERE ticker = 'APPL';
```
. Create an IMap to serve as a sink for the data generated by `high_low_enriched`.
+
Expand Down Expand Up @@ -413,15 +412,15 @@ AS SINK INTO current_trade
SELECT
troe.ticker AS __key,
troe.company AS company,
troe.price AS price,
ROUND(troe.price,2) AS price,
troe.shares AS shares,
hl.high AS high,
hl.low AS low,
ROUND(hl.high,2) AS high,
ROUND(hl.low,2) AS low,
troe.trade_ts
FROM tro_enriched AS troe
JOIN high_low AS hl
ON troe.ticker = hl.ticker
AND hl.window_end BETWEEN troe.trade_ts AND troe.trade_ts + INTERVAL '0.1' SECOND;
ON troe.ticker = hl.ticker
AND hl.window_end BETWEEN troe.trade_ts AND troe.trade_ts + INTERVAL '0.1' SECOND;

```
. Verify that entries are being added to the `current_trade` map. Run this query multiple times to verify that the data is changing.
Expand Down