If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > remove duplicates but display all columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-22-03, 21:18
deekay2003 deekay2003 is offline
Registered User
 
Join Date: May 2003
Posts: 11
remove duplicates but display all columns

hi i hav a table as such:
id fld1 fld2 updatedate
------------------------------
1 aaa aaa 14-jan-2003
2 bbb bbb 14-jan-2003
3 ccc ccc 15-jan-2003

and i wish to generate a result set that shows rows where the updatedate is unique but i want to include ALL columns in that result set. If there are rows with the same updatedate i just want the first one.

Last edited by deekay2003; 09-22-03 at 21:59.
Reply With Quote
  #2 (permalink)  
Old 09-23-03, 04:06
usha_nandini usha_nandini is offline
Registered User
 
Join Date: Sep 2003
Posts: 14
Hi,

Please try

select id, fld1, fld2, distinct updatedate from table name

I am not sure if it is possible to get all distinct columns without
lisiting all the column names in the query

Reg
Usha
Reply With Quote
  #3 (permalink)  
Old 09-23-03, 06:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
select id, fld1, fld2, updatedate
from yourtable zz
where id =
( select min(id)
from yourtable
where updatedate = zz.updatedate )

rudy
http://r937.com/
Reply With Quote
  #4 (permalink)  
Old 01-11-05, 15:44
fstop fstop is offline
Registered User
 
Join Date: Dec 2004
Posts: 22
In a similar vein, I would also like to remove duplicate rows while displaying all columns in the query. Clnumber is the column I want to key on for duplicates. Here is a very slimmed down (there are many more columns & joins) example of the query with some sample data:

select rolodex.ipname, clients.clnumber, classes.class, classes.date
from clients
join rolodex on clientid = rolodexid
join classes on clientid = classid
where rolodexid = 3

ipname clnumber class date
-----------------------------------
smith 11111 320 2004-12-21
smith 11111 420 2004-12-22
barnes 22222 320 2004-12-21
jones 33333 320 2004-12-21
jones 33333 320 2004-12-21

Here is what I want the results to be (I don't care which of the duplicate rows get eliminated, although I suppose I could use class with an aggregate like in Rudy's prior example):
ipname clnumber class date
-----------------------------------
smith 11111 320 2004-12-21
barnes 22222 320 2004-12-21
jones 33333 320 2004-12-21

I tried the following but got no results:
select * from
(
select rolodex.ipname, clients.clnumber, classes.class, classes.date
from clients
join rolodex on clientid = rolodexid
join classes on clientid = classid
where rolodexid = 3
) As IPN
Where class =
(Select MIN(class) FROM classes
where clnumber = IPN.clnumber)
Reply With Quote
  #5 (permalink)  
Old 01-12-05, 17:19
chuzhoi chuzhoi is offline
Registered User
 
Join Date: Dec 2002
Posts: 134
Quote:
Originally Posted by fstop
In a similar vein, I would also like to remove duplicate rows while displaying all columns in the query. Clnumber is the column I want to key on for duplicates.
Below is vendor specific(DB2) SQL using OLAP functions. I think Oracle should support the same syntax (except temp table).

with temp(ipname, clnumber, class, date) as
(values
('smith', 11111, 320, '2004-12-21'),
('smith', 11111, 420, '2004-12-22'),
('barnes', 22222, 320, '2004-12-21'),
('jones', 33333, 320, '2004-12-21'),
('jones', 33333, 320, '2004-12-21')
)

select ipname, clnumber, class, date from
( select
ipname, clnumber, class, date,
rownumber() over (partition by clnumber) as r
from temp) t
where r = 1


IPNAME CLNUMBER CLASS DATE
------ ----------- ----------- ----------
smith 11111 320 2004-12-21
barnes 22222 320 2004-12-21
jones 33333 320 2004-12-21

3 record(s) selected.
Reply With Quote
  #6 (permalink)  
Old 01-12-05, 17:41
fstop fstop is offline
Registered User
 
Join Date: Dec 2004
Posts: 22
Thanks for your reply, chuzhoi. I'm on SQL Server, so I'm not sure if this will work. However, I will look into it. I appreciate the effort.
Reply With Quote
  #7 (permalink)  
Old 01-12-05, 22:41
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
For MS-SQL, and assuming there were no time values in the updatedate column, I'd use:
Code:
SELECT *
   FROM myTable AS a
   WHERE  a.id = (SELECT Min(b.id)
      FROM myTable AS b
      WHERE  b.updatedate = a.updatedate)
-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On