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
Solved

Mysql get top rows

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

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

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

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle Query - Return results based on minimum value 8 35
error in my cursor 5 41
How to fix Datetime in MySQL? 4 48
Inserting data into database 10 34
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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