Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


2 newbie queries

2 newbie queries

2 newbie queries

There are 2 Mysql tables for Equities, [b]Characteristics[/b] and [b]Prices[/b].
>>> Characteristics has 3 columns: [b]Equity[/b], [b]AvgVol[/b] and [b]Cap[/b].
>>> Prices has 3 columns: [b]Equity[/b], [b]Date[/b] and [b]Price[/b].
I would like to create 2 Views, each with 4 columns:
>>> [b]Equity[/b], Price with Date = Date1 as [b]P1[/b], Price with Date = Date2 as [b]P2[/b] and [b]Gain[/b] = (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?

RE: 2 newbie queries

where do Date1 and Date2 come from?

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

RE: 2 newbie queries

They are explicitly input as part of the query

RE: 2 newbie queries


SELECT p.equity
     , p.p1 
     , p.p2
     , ( p.p2 - p.p1 ) / p.p1 AS gain
  FROM characteristics AS c
  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
             BY equity ) AS p
    ON p.equity = c.equity 
 WHERE c.cap >= 2 
   AND c.avgvol > 100000    
    BY gain DESC LIMIT 50 

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

RE: 2 newbie queries

I'm impressed; thanx,

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close