Solved

Mysql get top rows

Posted on 2013-11-17
10
298 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
10 Comments
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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
 

Author Comment

by:myyis
Comment Utility
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 40

Expert Comment

by:Sharath
Comment Utility
>> 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
 

Author Comment

by:myyis
Comment Utility
No I don't have.  The order with  ID (PK) is sufficient.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
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 48

Expert Comment

by:PortletPaul
Comment Utility
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
Comment Utility
@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
 

Author Closing Comment

by:myyis
Comment Utility
worked! great!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Creating and Managing Databases with phpMyAdmin in cPanel.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now