Solved

SQL Grouping issues

Posted on 2012-03-27
6
349 Views
Last Modified: 2012-04-04
This gives me 10 rows and then groups them...

SELECT FILG_DT, COUNT(FILG_DT) 
FROM DOC 
WHERE ROWNUM <= 10 AND FILG_DT IS NOT NULL
GROUP BY FILG_DT 
ORDER BY FILG_DT DESC;

Open in new window


How do I get 10 groups, regardless of rows?

What I am hoping to avoide is the select * from (select filg_dt from doc group by filg_dt) a rownum <= 10, because it will call 180 million records, before I just get my results of the top 10 groups.

Thanks much
0
Comment
Question by:Evan Cutler
[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
6 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 37774316
Try this:


SELECT FILG_DT, COUNT(FILG_DT)
FROM DOC
WHERE ROWNUM <= 10 AND FILG_DT IS NOT NULL
AND FILG_DT IN (SELECT TOP 10 FILG_DT FROM DOC ORDER BY FILG_DT)
GROUP BY FILG_DT
ORDER BY FILG_DT DESC;
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37774586
This was posted in an Oracle zone.  TOP 10 is not Oracle syntax.  Also not sure it sql server or others use ROWNUM.

>>rownum <= 10, because it will call 180 million records

rownum is a dynamic column and is applied before any order or group by.  You need to order the results first.

You can try the ROW_NUMBER window function but on 180 million rows, it might not be much faster:  Maybe the COUNT window function.

The problem is your examples do not match,  One does a count ordered by date, the other just the top 10 by date with no count.

If you can provide sample data and expected results we can probably come close to a real solution

Otherwise, here is the doc link:
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions156.htm#SQLRF06100
0
 
LVL 27

Expert Comment

by:tliotta
ID: 37774679
...results of the top 10 groups.

I assume you mean "first ten groups" based on descending sort of FILG_DT.

I am hoping to avoid ... , because it will call 180 million records

Is the table indexed over FILG_DT?

Tom
0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 9

Author Closing Comment

by:Evan Cutler
ID: 37805910
This one came to nearly exactly to what was needed.
thanks.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37805951
Really?

The syntax I posted was for MS Access (was the Oracle zone included by accident?).

Can you let us know what database you're using and the final syntax for the query you used?  That would be good information for others reading this thread.
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 37806043
SELECT doc.FILG_DT, COUNT(doc.FILG_DT)
FROM DOC
join (select * from (select distinct filg_dt from doc) a where rownum < 11) a on a.Filg_dt = doc.filg_dt GROUP BY doc.FILG_DT;

I know it looks different, but the premise is yours.  Find the top 11 dates distinctivly, then do a query based on that.
Had to do it in ORACLE speak.

Thanks :)
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

734 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