Solved

SQL Grouping issues

Posted on 2012-03-27
6
336 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
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 76

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 …
Hello, In my precious Article  (http://www.experts-exchange.com/Database/Reporting/A_15280-Create-Project-in-Microstrategy-Part-I.html)we saw the Configuration part for Microstrategy which included Metadata Creation and DataSource Preparation as …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

706 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

19 Experts available now in Live!

Get 1:1 Help Now