|
| 1 | +-- Same as above but for a given stock |
| 2 | +SELECT p.Symbol, |
| 3 | + p.PostHour, |
| 4 | + ROUND(MIN(c.AskPrice),2) MinAskPrice, |
| 5 | + ROUND(AVG(c.AskPrice),2) AvgAskPrice, |
| 6 | + ROUND(MAX(c.AskPrice),2) MaxAskPrice, |
| 7 | + ROUND(AVG(p.SentimentScore),3) AvgSentiment, |
| 8 | + ROUND(AVG(p.WeightedScore),2) WeightedScore, |
| 9 | + SUM(p.PostCount) PostCount, |
| 10 | + SUM(p.PositiveCount) Positives, |
| 11 | + SUM(p.NegativeCount) Negatives, |
| 12 | + SUM(p.NoiseCount) Noise |
| 13 | +FROM ( |
| 14 | + SELECT r.Symbol, |
| 15 | + FORMAT_TIMESTAMP("%F-%k", r.PostedOn) |
| 16 | + PostHour, r.SentimentScore, |
| 17 | + -- Using Friend Count as a weight multiplier for significant sentiment, else 0 |
| 18 | + (1 + a.FriendCount / 1000) * CASE |
| 19 | + -- if strong then full credit for score |
| 20 | + WHEN ABS(r.SentimentScore) > 0.6 THEN r.SentimentScore |
| 21 | + -- if poor give them 50% credit for the sentiment score |
| 22 | + WHEN ABS(r.SentimentScore) >= 0.3 AND ABS(r.SentimentScore) <= 0.6 THEN r.SentimentScore * 0.5 |
| 23 | + -- else it is noise to ignore |
| 24 | + ELSE 0 END WeightedScore, |
| 25 | + 1 PostCount, |
| 26 | + -- for positic/negatives using 0.3 significant sentiment filter, else noise |
| 27 | + CASE WHEN r.SentimentScore >= 0.3 THEN 1 ELSE 0 END PositiveCount, |
| 28 | + CASE WHEN r.SentimentScore <= -0.3 THEN 1 ELSE 0 END NegativeCount, |
| 29 | + CASE WHEN r.SentimentScore > -0.3 AND r.SentimentScore < 0.3 THEN 1 ELSE 0 END NoiseCount |
| 30 | + FROM Posts r |
| 31 | + JOIN Authors a ON r.Username = a.Username |
| 32 | + WHERE r.Symbol = 'MSFT' |
| 33 | +) p |
| 34 | +LEFT JOIN ( |
| 35 | + SELECT Symbol, FORMAT_TIMESTAMP("%F-%k", SampleOn) AskHour, AskPrice |
| 36 | + FROM Prices |
| 37 | +) c ON p.Symbol = c.Symbol AND p.PostHour = c.AskHour |
| 38 | +GROUP BY p.Symbol, p.PostHour |
| 39 | +ORDER BY p.Symbol, p.PostHour DESC |
0 commit comments