?
Solved

How to add a sequential row number column to an aggregate view.

Posted on 2010-04-02
4
Medium Priority
?
558 Views
Last Modified: 2013-12-07
I have a query  that agrregates the values from a table in oracle. I want to add a column with sequential rownumbers and I get the following error:

"Ora-00979 Not a valid Group by expression."

It is trying to pull the row numbers from the source data. What  I want is to have a squential id number colum.

SELECT ROWNUM,
         COUNT (DISTINCT COVERPAGE_GIS.PARENT_COMPANY) COMPANIES,
         FORM477_PART_VI_GIS.TRACT_FIPS
    FROM COVERPAGE_GIS, FORM477_PART_VI_GIS
   WHERE (COVERPAGE_GIS.CONTROL_ID = FORM477_PART_VI_GIS.CONTROL_ID)
         AND ( (FORM477_PART_VI_GIS.UPLOAD_RATE_CODE >= 2)
              OR (FORM477_PART_VI_GIS.DOWNLOAD_RATE_CODE >= 2))
GROUP BY FORM477_PART_VI_GIS.TRACT_FIPS
0
Comment
Question by:OR1
  • 2
4 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 29461401
Try this:
SELECT ROWNUM,
         COUNT (DISTINCT COVERPAGE_GIS.PARENT_COMPANY) OVER ( partition by  FORM477_PART_VI_GIS.TRACT_FIPS) COMPANIES,
         FORM477_PART_VI_GIS.TRACT_FIPS
    FROM COVERPAGE_GIS, FORM477_PART_VI_GIS
   WHERE (COVERPAGE_GIS.CONTROL_ID = FORM477_PART_VI_GIS.CONTROL_ID)
         AND ( (FORM477_PART_VI_GIS.UPLOAD_RATE_CODE >= 2)
              OR (FORM477_PART_VI_GIS.DOWNLOAD_RATE_CODE >= 2))

Open in new window

0
 
LVL 17

Accepted Solution

by:
k_murli_krishna earned 200 total points
ID: 29461621
SELECT ROWNUM, COMPANIES, TRACT_FIPS
FROM(SELECT
         COUNT (DISTINCT COVERPAGE_GIS.PARENT_COMPANY) COMPANIES,
         FORM477_PART_VI_GIS.TRACT_FIPS
    FROM COVERPAGE_GIS, FORM477_PART_VI_GIS
   WHERE (COVERPAGE_GIS.CONTROL_ID = FORM477_PART_VI_GIS.CONTROL_ID)
         AND ( (FORM477_PART_VI_GIS.UPLOAD_RATE_CODE >= 2)
              OR (FORM477_PART_VI_GIS.DOWNLOAD_RATE_CODE >= 2))
GROUP BY FORM477_PART_VI_GIS.TRACT_FIPS)iq;

You can remove iq if there is a problem or use with AS iq. Not to sure about this.
0
 

Author Closing Comment

by:OR1
ID: 31710279
Your solution works with or without the iq. Can you tell me the reason for it?

Thanks
0
 
LVL 17

Expert Comment

by:k_murli_krishna
ID: 29480271
In Oracle iq is optional. In DB2, iq or AS iq is compulsory for nested query (overall/select list/where/having) i.e. normal OR correlated.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article, we will show how to detach and attach a database and then show how to repair a corrupt database and attach it, If it has some errors. We will show how to detach and attach using SSMS or using T-SQL sentences.
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

601 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