linehanp

CA_660_Assignment_2

Dec 31st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.40 KB | None | 0 0
  1. Query_1
  2.  
  3. SELECT
  4.   c.sp_1,
  5.   f.fi_company_name,  -- <<< not strictly necessary, but good to have the name!
  6.   c.first_6_avg AS FIRST,
  7.   c.last_6_avg AS LAST,  
  8.   c.last_6_avg - c.first_6_avg AS diff,
  9.   (c.last_6_avg::DECIMAL/c.first_6_avg::DECIMAL)::DECIMAL(6,2) * 100 AS percentage_rise -- <<-- the CRITICAL measurement
  10. FROM comp_price c                     -- <<<--- this table I created by inserting the av. price over 6 months 5 years ago (first)
  11. JOIN financial_info f                 --                            and inserting the av. price for the most recent 6 months (last).                  
  12.   ON (c.sp_1 = f.fi_company_symbol)
  13. ORDER BY percentage_rise DESC          --  <<<--- Sorting by this critical measurement
  14. LIMIT 50 OFFSET 200;
  15.  
  16. Query_2
  17.  
  18.  
  19. SELECT SUM(volatility_metric)::DECIMAL(7,2) AS "Total Volatility" <<== my metric
  20. FROM
  21. (
  22. SELECT
  23.   s.company_code,
  24.   AVG(s.low_price)::DECIMAL(15, 2) AS low_average,
  25.   AVG(s.high_price)::DECIMAL(15,2) AS high_average,
  26.   ((AVG(s.high_price)::DECIMAL(15,2) + AVG(s.low_price)::DECIMAL(15,2))/2)::DECIMAL(10,2) AS average_mean,  
  27.   (SUM((((s.high_price::DECIMAL + s.low_price::DECIMAL)/2) - s.open_price)^2))::DECIMAL(15,2) AS stuff,  -- <<-- help for sigma!
  28.   (SQRT(SUM((((s.high_price::DECIMAL + s.low_price::DECIMAL)/2) - s.open_price)^2)/COUNT(s.company_code)))::DECIMAL(15,2) AS std_deviation,  
  29.   (((AVG(s.high_price)::DECIMAL(15,2) + AVG(s.low_price)::DECIMAL(15,2))/2)::DECIMAL(10,2))/((SQRT(SUM((((s.high_price::DECIMAL + s.low_price::DECIMAL)/2) - s.open_price)^2)/COUNT(s.company_code)))::DECIMAL(15,2)) AS volatility_metric,
  30.  
  31.   COUNT(s.company_code)
  32. FROM share s
  33. WHERE s.company_code IN
  34. (
  35.   SELECT the_code FROM
  36.   (
  37.     -- Most of the fields here are superfluous - I just didn't want to delete them in case
  38.     -- they came in useful later on - but, time waits for no man!
  39.     SELECT
  40.     c.sp_1 AS the_code,
  41.     f.fi_company_name,
  42.     c.first_6_avg AS FIRST,
  43.     c.last_6_avg AS LAST,  
  44.     c.last_6_avg - c.first_6_avg AS diff,
  45.     (c.last_6_avg::DECIMAL/c.first_6_avg::DECIMAL)::DECIMAL(6,2) * 100 AS percentage_rise
  46.     FROM comp_price c
  47.     JOIN financial_info f
  48.       ON (c.sp_1 = f.fi_company_symbol)
  49.     ORDER BY percentage_rise DESC
  50.     LIMIT 50 OFFSET 398   -- <<<<   Vary these numbers to obtain the Volatility Coefficient for various cohorts!
  51.   ) AS x
  52. )
  53. GROUP BY s.company_code
  54. ORDER BY company_code
  55. ) AS y;
Add Comment
Please, Sign In to add comment