0

I have a table which may contain around 3 million data, I need query to fetch around 300k data with status 'inactive' say all regions having 30k data and export into csv file.

select Id, Name, region, status, creationdate 
from Organization 
where status = 'inactive' 
group by region
having count = 30000
order by creationdate desc;

Does this query fetch the latest data in all regions or need to be fine tuned?

The Impaler
  • 45,731
  • 9
  • 39
  • 76
kumar
  • 37
  • 7
  • The query will not run you will get `ORA-00979: not a GROUP BY expression` – MT0 Jun 22 '22 at 07:56
  • Does this answer your question? [Select First Row of Every Group in sql](https://stackoverflow.com/questions/16529701/select-first-row-of-every-group-in-sql) – astentx Jun 22 '22 at 10:43

1 Answers1

4

You can try this one:

SELECT * 
  FROM (select Id, Name, region, status, creationdate,
             ROW_NUMBER() OVER (PARTITION BY REGION ORDER BY creationdate DESC) as ROW_NUM 
        from Organization 
       where status='inactive')
WHERE ROW_NUM <= 30000
MT0
  • 143,790
  • 11
  • 59
  • 117
HRK
  • 336
  • 1
  • 2
  • 8