?
Solved

Mysql get top rows

Posted on 2013-11-17
10
Medium Priority
?
345 Views
Last Modified: 2013-11-21
I want get ( the rows which are not the last 2000 rows) from the ones  having more than 2000 rows.
How can I do?

SELECT (the ones which are not last 2000) FROM FEED WHERE ORID IN (
SELECT ORID
FROM  `FEED`
GROUP BY ORID
HAVING COUNT( * ) >2000)
0
Comment
Question by:myyis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39654802
Are you looking for this?
SELECT * FROM (
SELECT ORID,COUNT(*) cnt
FROM  `FEED`
GROUP BY ORID
HAVING COUNT( * ) >2000) t1
ORDER BY cnt DESC
LIMIT 2000
0
 
LVL 1

Author Comment

by:myyis
ID: 39655173
I think you query gives me the  count (*). But I need the rows of Feed table itselves.
eg.

Lets say the FEED has fields  ID (PK)  and ORID,
it has
2500 records with ORID=1
1500 records with ORID=2  
3700 records with ORID=3

I need to have the list of records
First 500 records with  ORID=1 UNION First 1700 records with  ORID=3
 (Nothing from ORID=2 since #of records are less than 2000)
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39658514
>> First 500 records with  ORID=1 UNION First 1700 records with  ORID=3

Do you have any date field like Update date, Modified date to order the records?
0
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 1

Author Comment

by:myyis
ID: 39658664
No I don't have.  The order with  ID (PK) is sufficient.
0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 39664702
Can you try this?
SELECT ORDID, 
       ID 
  FROM (SELECT *, 
               IF(@ORID = ORID, @rownum := @rownum + 1, @rownum := 1) AS RowNum, 
               IF(@ORID <> ORID, @ORID := ORID, -999)                 s 
          FROM (SELECT * 
                  FROM FEED 
                 WHERE ORID IN (SELECT ORID 
                                  FROM FEED 
                                 GROUP BY ORID 
                                HAVING COUNT(*) > 2000)) t1, 
               (SELECT @rownum := 1, 
                       @ORID := -999) r 
         ORDER BY ORID, 
                  ID DESC) t2 
 WHERE RowNum > 2000 
 ORDER BY ORID, 
          ID;

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39664739
Lets say the FEED has fields  ID (PK)  and ORID,
it has
2500 records with ORID=1
1500 records with ORID=2  << ignored
3700 records with ORID=3

I need to have the list of records
First 500 records with  ORID=1 UNION First 1700 records with  ORID=3
(500+1,700) = 2,200 :: why 2,200 records?

also:
why 500 of 2,500 ? (20% of that ordid)
why 1,700 of 3,700 ? (46% of that ordid)
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 39666713
@PortletPaul, he needs the records excluding the first 2000, he stated this in the original post. So if an ORID has < 2000 records it would not return any rows.

this may be possible but you would need better than the community version of mysql so that you can put a LIMIT into a subquery.

Example
create a view with this as the sql statement

select ORID, count(ORID) as feed_count from feed 
group by ORID

Open in new window


then make a second view (get the last 2000 records)

Select f1.id from feed as f1 where 2000 <
(Select feed_count from name_of_your_view as f2 where f2.ORID = f1.ORID)
order by ID desc

Open in new window


then your query

Select * from feed f1 where ID in (select id from name_of_second_view f2 where f1.ORID = f2.ORID LIMIT 2000,18446744073709551615)
order by ORID asc

Open in new window


I think this query would also take a very long time to run.
0
 
LVL 1

Author Closing Comment

by:myyis
ID: 39666785
worked! great!
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question