473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Why we used ROWNUM in group by clause

If we want to retrive 5,6,7 rows from a table,then we used rownum in a query in group by clause,but my doubt is rownum has individual values for each rows then how did you group rows based on rownum?
Oct 13 '07 #1
2 36199
debasisdas
8,127 Expert 4TB
Unlike ROWID ,ROWNUM is not fixed for every row. This is the serial number of the row in the recordset (cursor), not in the table. So ROWNUM is assigned after group by not before that.
Oct 13 '07 #2
gintsp
36
So ROWNUM is assigned after group by not before that.
Mhmm that's not true:
Expand|Select|Wrap|Line Numbers
  1. SQL> ed
  2. Wrote file afiedt.buf
  3.  
  4.   1  select rownum, owner, count(*)
  5.   2  from dba_source
  6.   3  where rownum <=10
  7.   4* group by rownum, owner
  8. SQL> /
  9.  
  10.     ROWNUM OWNER                            COUNT(*)
  11. ---------- ------------------------------ ----------
  12.          4 SYS                                     1
  13.          6 SYS                                     1
  14.          2 SYS                                     1
  15.          3 SYS                                     1
  16.          5 SYS                                     1
  17.          8 SYS                                     1
  18.          9 SYS                                     1
  19.          7 SYS                                     1
  20.          1 SYS                                     1
  21.         10 SYS                                     1
  22.  
  23. 10 rows selected.
  24.  
  25. SQL> ed
  26. Wrote file afiedt.buf
  27.  
  28.   1  select rownum , owner, cnt from (
  29.   2    select owner, count(*) cnt
  30.   3    from dba_source
  31.   4    where rownum <=10
  32.   5    group by owner
  33.   6* )
  34. SQL> /
  35.  
  36.     ROWNUM OWNER                                 CNT
  37. ---------- ------------------------------ ----------
  38.          1 SYS                                    10
If you need rownum for each group then you should do group by in inner select and assign rownum in outer.

Gints Plivna
http://www.gplivna.eu
Oct 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul Bradford | last post by:
I have a query that takes about two minutes and returns 97 rows. If I change the query only by adding ROWNUM to the outermost select clause, the query never returns (I let it run overnight). I'm...
6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
9
by: Sandy | last post by:
I am trying to do the following: EXEC SQL UPDATE MY TABLE SET COL1 = :newValue WHERE COL1 = 0 AND ROWNUM = 1 ORDER BY COL2; (index on COL1, COL2)
3
by: Robby McGehee | last post by:
I need this to work: SELECT FROM WITH (NOLOCK) where ='a' GROUP BY , HAVING COUNT () > 1 The problem is that I get an error that needs to be in the GROUP BY clause or aggregate function. if...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
4
by: pierig.gueguen | last post by:
Hello, I would like to know if the equivalent Oracle rownum exist in SQLServer. Here is a sample SQL code to explain what I want to do : select jobs.name, jobs.job_id, jobs.description,...
12
by: Bill Moran | last post by:
Hey all. I've hit an SQL problem that I'm a bit mystified by. I have two different questions regarding this problem: why? and how do I work around it? The following query: SELECT GCP.id,...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
0
by: Walt | last post by:
Darta wrote: I haven't read the question in detail, so this may be way off base, but have you considered using rowid instead of rownum? rownum is a psuedo-column that's a bit "slippery", i.e....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.