I am relatively inexperienced with SQL, and I am trying to learn how
to analyze some data with it.
I have a table with the following information.
COMPANY ID , DATE, MarektValue
I would like to select the largest 100 marketvalues for each date in
my table. How can I do this efficiently in MySql.
In Microsoft Access , I can run something like this, but it is
extremely slow:
SELECT Company ID, Date, MarketValue
FROM Table1 RIGHT JOIN Dates ON Table1.Date = Dates.Date
WHERE (((Marketvalue) In (Select Top 100 [marketvalue] from Table1
where [Date] = [dates].[Date] Order by date, MarketValue Desc)));
I know that the LIMIT function in mySql will get 100 rows if i am
looking at one specific year. but I cant seem to get it to take the
100 largest marketvalues every year.
Thanks in advance for any assistance! 5 2076
Jeremy wrote: I am relatively inexperienced with SQL, and I am trying to learn how to analyze some data with it.
I have a table with the following information.
COMPANY ID , DATE, MarektValue
I would like to select the largest 100 marketvalues for each date in my table. How can I do this efficiently in MySql.
In Microsoft Access , I can run something like this, but it is extremely slow:
SELECT Company ID, Date, MarketValue FROM Table1 RIGHT JOIN Dates ON Table1.Date = Dates.Date WHERE (((Marketvalue) In (Select Top 100 [marketvalue] from Table1 where [Date] = [dates].[Date] Order by date, MarketValue Desc)));
I know that the LIMIT function in mySql will get 100 rows if i am looking at one specific year. but I cant seem to get it to take the 100 largest marketvalues every year.
Thanks in advance for any assistance!
Hey Jeremy,
Not to get to in depth with your query have you tried using the LIMIT CLAUSE along with the ORDER BY clause.
I would just guess off the top of my head (Sorry leaving work right now...Can't wait to get home!!!)
SELECT Company ID, Date, MarketValue
FROM Table1 RIGHT JOIN Dates ON Table1.Date = Dates.Date
WHERE [Date] = [dates].[Date]
ORDER BY MarketValue Desc (or however you feel like ordering by)
LIMIT 0, 100
.... Like I said I am on my way out, but I will revisit this as soon as I get in. Let me know if this query did it for you
I think my problem is that I am using SQLyog, which supports MySQl
version 4.17. I tried running the following without success:
DECLARE @i int
SET @i 1960
WHILE @i < 1965
BEGIN
SET @i = @i +1
SELECT Date, CompanyID, marketvalue FROM Table1 Where
(Month(date)=12 and Year(date)=@i) Order by Date, marketvalue Desc
Limit 1,100
END
Any ideas?
Jeremy wrote: I am relatively inexperienced with SQL, and I am trying to learn how to analyze some data with it.
I have a table with the following information.
COMPANY ID , DATE, MarektValue
I would like to select the largest 100 marketvalues for each date in my table. How can I do this efficiently in MySql.
In Microsoft Access , I can run something like this, but it is extremely slow:
SELECT Company ID, Date, MarketValue FROM Table1 RIGHT JOIN Dates ON Table1.Date = Dates.Date WHERE (((Marketvalue) In (Select Top 100 [marketvalue] from Table1 where [Date] = [dates].[Date] Order by date, MarketValue Desc)));
I know that the LIMIT function in mySql will get 100 rows if i am looking at one specific year. but I cant seem to get it to take the 100 largest marketvalues every year.
Thanks in advance for any assistance!
Collective,
Thanks for trying. I have tried the limit clause as per your
suggestion. So far, I have only been able to return the 100 rows for
whatever dates are specified. what i need the query to do is return the
top 100 rows for every year specified. IE, if Dates were December 1960
and December 1961, I would get 100 results from 1960 and 1961. I
imagine it needs some type of loop function. I am using SQLyog as my
front-end application, which i think supports SQL 4.17. Someone suggest
a WHILE statement, but that didnt work either. Any additional
suggestions are much appreciated.
The Collective wrote: Jeremy wrote: I am relatively inexperienced with SQL, and I am trying to learn
how to analyze some data with it.
I have a table with the following information.
COMPANY ID , DATE, MarektValue
I would like to select the largest 100 marketvalues for each date
in my table. How can I do this efficiently in MySql.
In Microsoft Access , I can run something like this, but it is extremely slow:
SELECT Company ID, Date, MarketValue FROM Table1 RIGHT JOIN Dates ON Table1.Date = Dates.Date WHERE (((Marketvalue) In (Select Top 100 [marketvalue] from Table1 where [Date] = [dates].[Date] Order by date, MarketValue Desc)));
I know that the LIMIT function in mySql will get 100 rows if i am looking at one specific year. but I cant seem to get it to take the 100 largest marketvalues every year.
Thanks in advance for any assistance!
Hey Jeremy,
Not to get to in depth with your query have you tried using the LIMIT
CLAUSE along with the ORDER BY clause.
I would just guess off the top of my head (Sorry leaving work right
now...Can't wait to get home!!!) SELECT Company ID, Date, MarketValue FROM Table1 RIGHT JOIN Dates ON Table1.Date = Dates.Date WHERE [Date] = [dates].[Date] ORDER BY MarketValue Desc (or however you feel like ordering by) LIMIT 0, 100
... Like I said I am on my way out, but I will revisit this as soon
as I get in. Let me know if this query did it for you je******@gmail.com wrote: I think my problem is that I am using SQLyog, which supports MySQl version 4.17. I tried running the following without success:
DECLARE @i int SET @i 1960 WHILE @i < 1965 BEGIN SET @i = @i +1 SELECT Date, CompanyID, marketvalue FROM Table1 Where (Month(date)=12 and Year(date)=@i) Order by Date, marketvalue Desc Limit 1,100
END
Two problems with that:
1. It looks like the stored procedure language. You can use this only
when defining a stored procedure. This works only in MySQL 5.x.
2. Even so, it's incorrect syntax for MySQL's stored procedure language.
MySQL doesn't use the @ symbols, as Microsoft SQL Server does.
Bill K.
Jeremy wrote: SELECT Company ID, Date, MarketValue FROM Table1 RIGHT JOIN Dates ON Table1.Date = Dates.Date WHERE (((Marketvalue) In (Select Top 100 [marketvalue] from Table1 where [Date] = [dates].[Date] Order by date, MarketValue Desc)));
what i need the query to do is return the top 100 rows for every year specified
If your SQLyog front-end supports MySQL 4.1, and you are in fact running
MySQL 4.1 (required for subqueries), I think this might do what you want:
SELECT T1.`COMPANY ID`, T1.`Date`, T1.MarketValue
FROM Table1 AS T1 INNER JOIN
(SELECT DISTINCT YEAR(`Date`) AS YEAR FROM Dates) AS D
ON YEAR(T1.`Date`) = D.YEAR
WHERE T1.MarketValue IN
(SELECT DISTINCT T2.MarketValue FROM Table1 AS T2
WHERE T2.`COMPANY ID` = T1.`COMPANY ID`
AND YEAR(T2.`Date`) = YEAR(T1.`Date1)
ORDER BY T2.MarketValue DESC
LIMIT 100)
NB: this is not tested.
Further reading: http://dev.mysql.com/doc/mysql/en/AN...ubqueries.html http://dev.mysql.com/doc/mysql/en/Unnamed_views.html (subqueries in the
FROM clause) http://dev.mysql.com/doc/mysql/en/SELECT.html (read about LIMIT and
DISTINCT) http://dev.mysql.com/doc/mysql/en/Da...functions.html (the YEAR
function)
Regards,
Bill K. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: joe |
last post by:
Can anyone help me out by either telling me how to get to the result i
need or by pointing me to some documentation about the following question:
A certain query to a database give me eg 100...
|
by: Lars |
last post by:
I am trying to post a pretty long string (9300 bytes) in a
hidden field and i get this very strange error message:
Request object, ASP 0107 (0x800A01B8)
The data being processed is over the...
|
by: mrwoopey |
last post by:
Hi,
My OLAP data cube is giving me the following error when I am
manipulating OLAP data cube views:
"the data being processed is over the allowed limit"
I know that this message is caused by...
|
by: krystoffff |
last post by:
Hi
I would like to paginate the results of a query on several pages. So I
use a query with a limit X offset Y to display X results on a page,
ok.
But for the first page, I need to run the...
|
by: bibsoconner |
last post by:
Hi,
I hope someone can please help me. I'm having a lot of trouble with
schema files in .NET. I have produced a very simple example that uses
"include" to include other schema files. It all...
|
by: paulw |
last post by:
Hi
Please give problems that "HAS TO" to use recursion (recursive calls to
itself.)
Preferrably real world examples, not knights tour.
I'm thinking about eliminating the use of stack...
...
|
by: TG |
last post by:
I'm using the System.DirectoryServices.DirectorySearcher.Findall
method to attempt to retrieve user id's from an Exchange LDAP server.
I get the error message "The administrative limit for this...
|
by: Martin |
last post by:
I'm trying to adapt a PHP script that was written to use MySQL, so
that it will work with an MSAccess MDB file.
An important part of the script makes use of the SQL "LIMIT" keyword
available in...
|
by: tekctrl |
last post by:
Anyone:
I have a simple MSAccess DB which was created from an old ASCII flatfile.
It works fine except for something that just started happening. I'll enter
info in a record, save the record,...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |