I am relatively inexperienced with mySQL, and I am trying to learn how
to analyze some data with it. I am using the latest free version of
SQLYOG available.
I have a table with the following information on a monthly basis.
COMPANYID , DATE, MarektValue
I would like to select the largest 100 marketvalues on Decemeber of every year.
If I were to do this for one year, say 1960, it is easily written as:
SELECT Date, CompanyID, marketvalue FROM Table1 Where (Month(date)=12
and Year(date)=1960) Order by Date, MarketValue Desc Limit 1,100
But I want to do this for a flexible range of years, say 1960-2003. It
seems like you should be able to do something with a LOOP and END LOOP
that would allow you to run the above select statement For Years =
1960 to
2003 or something to that effect, but I don't know where to begin. |
Request for Question Clarification by
andyt-ga
on
02 Dec 2004 11:26 PST
jeremyd2-ga,
I'm posting this as a clarification because I'm not sure it's what you
wanted (ie: all the records in one query), but let me know if it
answers the question:
SELECT Date, CompanyID, marketvalue FROM Table1 Where (Month(date)=12
and Year(date)>1960 and Year(date) < 2003 ) Order by Date, MarketValue
Desc Limit 1,100
--andyt-ga
|
Request for Question Clarification by
hammer-ga
on
09 Dec 2004 17:11 PST
Jeremyd2,
I don't have the right version of mySQL to test with (>=4.0.0), but
try using a UNION. Something like this:
(SELECT Date, CompanyID, marketvalue FROM Table1 Where (Month(date)=12
and Year(date)=1960) Order by MarketValue, Date Desc Limit 100) UNION
(SELECT Date, CompanyID, marketvalue FROM Table1 Where (Month(date)=12
and Year(date)=1961) Order by MarketValue, Date Desc Limit 100) UNION
(SELECT Date, CompanyID, marketvalue FROM Table1 Where (Month(date)=12
and Year(date)=1962) Order by MarketValue, Date Desc Limit 100);
MySQL Manual | 13.1.7.2 UNION Syntax
http://dev.mysql.com/doc/mysql/en/UNION.html
- Hammer
|