473,394 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Question Regarding "Limit"

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!
Jul 20 '05 #1
5 2093
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
Jul 20 '05 #2
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!


Jul 20 '05 #3
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

Jul 20 '05 #4
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.
Jul 20 '05 #5
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.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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...
2
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...
0
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...
0
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...
4
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...
10
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... ...
2
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...
5
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...
25
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,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.