Solved

SQL Grouping issues

Posted on 2012-03-27
6
350 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Suggested Courses

626 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