Solved

Mysql get top rows

Posted on 2013-11-17
10
322 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 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
 

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 500 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 48

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
 

Author Closing Comment

by:myyis
ID: 39666785
worked! great!
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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