Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 newbie queries

Status
Not open for further replies.

jalea148

Technical User
Jun 12, 2006
5
US
There are 2 Mysql tables for Equities, Characteristics and Prices.
>>> Characteristics has 3 columns: Equity, AvgVol and Cap.
>>> Prices has 3 columns: Equity, Date and Price.
I would like to create 2 Views, each with 4 columns:
>>> Equity, Price with Date = Date1 as P1, Price with Date = Date2 as P2 and Gain = (P2 – P1)/P1.
View1 has the Top 50 ordered by Gain, Descending with Cap >= 2 and AvgVol > 100000
View2 has the Bottom 50 ordered by Gain, Ascending with Cap >= 2 and AvgVol > 100000
How should these Queries be written?
Thanx,
Jay
 
They are explicitly input as part of the query
 
Code:
CREATE VIEW view1
AS
SELECT p.equity
     , p.p1 
     , p.p2
     , ( p.p2 - p.p1 ) / p.p1 AS gain
  FROM characteristics AS c
INNER
  JOIN ( SELECT equity
              , MAX(CASE WHEN `date` = '2012-09-09' -- date1
                         THEN price
                         ELSE NULL END ) AS p1 
              , MAX(CASE WHEN `date` = '2012-11-11' -- date2
                         THEN price
                         ELSE NULL END ) AS p2 
           FROM prices
          WHERE `date` IN ( '2012-09-09' , '2012-11-11' ) -- date1,date2
         GROUP
             BY equity ) AS p
    ON p.equity = c.equity 
 WHERE c.cap >= 2 
   AND c.avgvol > 100000    
ORDER
    BY gain DESC LIMIT 50

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top