dbTalk Databases Forums  

How to avoid duplicates here...?

comp.databases.oracle.server comp.databases.oracle.server


Discuss How to avoid duplicates here...? in the comp.databases.oracle.server forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Spare Brain
 
Posts: n/a

Default How to avoid duplicates here...? - 04-07-2004 , 09:16 PM






Hi,

I had a problem where I had to limit the rows returned - return only the
rows between N and M. I accomplished it using a SQL that looks something
like this:


SELECT rownum, emp_name from EMPLOYEE
WHERE dept = 'hardware'
group by rownum, emp_name having rownum between 10 and 15

Now, as it so happens, there could be multiple entries for any given
employee. I am getting multiple rows for the same employee! Is there a way
to introduce a "distinct" on just the emp_name? How else to achieve this? If
I use "select distinct rownum, emp_name..." it does not prevent multiple
employees from showing up!

Please post your valuable suggestions to the newsgroup.

Thanks!
SB



Reply With Quote
  #2  
Old   
Daniel Morgan
 
Posts: n/a

Default Re: How to avoid duplicates here...? - 04-07-2004 , 09:50 PM






Spare Brain wrote:

Quote:
Hi,

I had a problem where I had to limit the rows returned - return only the
rows between N and M. I accomplished it using a SQL that looks something
like this:


SELECT rownum, emp_name from EMPLOYEE
WHERE dept = 'hardware'
group by rownum, emp_name having rownum between 10 and 15

Now, as it so happens, there could be multiple entries for any given
employee. I am getting multiple rows for the same employee! Is there a way
to introduce a "distinct" on just the emp_name? How else to achieve this? If
I use "select distinct rownum, emp_name..." it does not prevent multiple
employees from showing up!

Please post your valuable suggestions to the newsgroup.

Thanks!
SB
Please don't post to multiple groups: One is enough.

SELECT emp_name
FROM (
SELECT DISTINCT emp_name, rownum
FROM employee
WHERE dept = 'hardware')
WHERE rownum BETWEEN 10 AND 15;

--
Daniel Morgan
http://www.outreach.washington.edu/e...ad/oad_crs.asp
http://www.outreach.washington.edu/e...oa/aoa_crs.asp
damorgan@x.washington.edu
(replace 'x' with a 'u' to reply)



Reply With Quote
  #3  
Old   
Kalmact
 
Posts: n/a

Default Re: How to avoid duplicates here...? - 04-08-2004 , 03:12 AM



Daniel Morgan <damorgan@x.washington.edu> wrote

Quote:
Spare Brain wrote:

Hi,

I had a problem where I had to limit the rows returned - return only the
rows between N and M. I accomplished it using a SQL that looks something
like this:


SELECT rownum, emp_name from EMPLOYEE
WHERE dept = 'hardware'
group by rownum, emp_name having rownum between 10 and 15

Now, as it so happens, there could be multiple entries for any given
employee. I am getting multiple rows for the same employee! Is there a way
to introduce a "distinct" on just the emp_name? How else to achieve this? If
I use "select distinct rownum, emp_name..." it does not prevent multiple
employees from showing up!

Please post your valuable suggestions to the newsgroup.

Thanks!
SB

Please don't post to multiple groups: One is enough.

SELECT emp_name
FROM (
SELECT DISTINCT emp_name, rownum
FROM employee
WHERE dept = 'hardware')
WHERE rownum BETWEEN 10 AND 15;

This would not prevent the duplicates from showing up since rownum
will make it unique. The correct solution would be
select emp_name from (
select rownum rnum,emp_name
from
(
select distinct(emp_name)
from employee
where dept = 'hardware' ) )
where rnum between 10 and 15


Reply With Quote
  #4  
Old   
Mark D Powell
 
Posts: n/a

Default Re: How to avoid duplicates here...? - 04-08-2004 , 08:24 AM



"Spare Brain" <spare_brain (AT) yahoo (DOT) com> wrote

Quote:
Hi,

I had a problem where I had to limit the rows returned - return only the
rows between N and M. I accomplished it using a SQL that looks something
like this:


SELECT rownum, emp_name from EMPLOYEE
WHERE dept = 'hardware'
group by rownum, emp_name having rownum between 10 and 15

Now, as it so happens, there could be multiple entries for any given
employee. I am getting multiple rows for the same employee! Is there a way
to introduce a "distinct" on just the emp_name? How else to achieve this? If
I use "select distinct rownum, emp_name..." it does not prevent multiple
employees from showing up!

Please post your valuable suggestions to the newsgroup.

Thanks!
SB
SB, please do not cross-post; it is very bad form.

You appear to be misusing the Oracle rownum psuedo column. See the
SQL manual for it's proper use and limitations.

In general to do what you want you have to select from a select that
assigns the limit values sort of like:

select col_list from (
select row_ctr, .... from table where .... order by row_ctr )
where row_ctr >= :N1
and row_ctr <= :N2

HTH -- Mark D Powell --


Reply With Quote
  #5  
Old   
Dave
 
Posts: n/a

Default Re: How to avoid duplicates here...? - 04-08-2004 , 08:37 AM



"Spare Brain" <spare_brain (AT) yahoo (DOT) com> wrote

Quote:
Hi,

I had a problem where I had to limit the rows returned - return only the
rows between N and M. I accomplished it using a SQL that looks something
like this:


SELECT rownum, emp_name from EMPLOYEE
WHERE dept = 'hardware'
group by rownum, emp_name having rownum between 10 and 15

Now, as it so happens, there could be multiple entries for any given
employee. I am getting multiple rows for the same employee! Is there a way
to introduce a "distinct" on just the emp_name? How else to achieve this? If
I use "select distinct rownum, emp_name..." it does not prevent multiple
employees from showing up!

Please post your valuable suggestions to the newsgroup.

Thanks!
SB
select emp_name
from
(
select emp_name, rownum rnum
from
(
select distinct emp_name
from employee
where dept = 'hardware'
ORDER BY emp_name
)
where rownum < 16
)
where rnum > 9

Dave


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.