Solved

SQL Grouping issues

Posted on 2012-03-27
6
339 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

867 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

15 Experts available now in Live!

Get 1:1 Help Now