Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Grouping issues

Posted on 2012-03-27
6
Medium Priority
?
352 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

719 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