July 15, 2008 at 4:44 am
Hi
I am sorry if someone has already posted this question and am asking again.
This problem is eating my head, I have a table
Table 1 -
1br
1er
2rt
3yh
2uj
3iu
3ol
Now I want only 1 row for each unique id, that row could be any row but id shouldn't repeat.
1br
2rt
3yh
or
1er
2uj
3ol
Thanks
July 15, 2008 at 5:37 am
use distinct keyword
karthik
July 15, 2008 at 5:59 am
Try something like this:
select distinct id, min(value) value
from
(select '1' id,'br' value
union
select '1','er'
union
select '2','rt'
union
select '2','uj'
union
select '3','iu'
union
select '3','ol') data
group by id
-- Output:
idvalue
1br
2rt
3iu
July 15, 2008 at 6:47 am
Thanks min max functions are an easy way to do it
July 15, 2008 at 7:26 am
Ooppsss I didn't see in which forum we are discussing ... sorry!
I posted the technique for the SQL Server 2005!
there is one of the solutions:
you should add the column as identity column incremented by one and performing the delete operation like this:
--ADDING THE IDENTITY COLUMN
ALTER TABLE table1
ADD IDENT INT IDENTITY(1,1);
GO
after that you will write this code:
DELETE FROM table1 WHERE IDENT NOT IN (SELECT MIN(IDENT) FROM TABLE1 GROUP BY ID);
SELECT * FROM TABLE1 --YOU WILL FIND THE DISTINCT VALUES OF YOUR TABLE
these techniques posted many time in many locations, also in SSC!
:w00t:
July 15, 2008 at 8:29 am
Even in SQL Server 2000, which is the this database forum?
N 56°04'39.16"
E 12°55'05.25"
July 15, 2008 at 8:55 am
karthikeyan (7/15/2008)
use distinct keyword
Let's see how you would do that, Karthik... 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
July 15, 2008 at 9:21 am
You can actually get rid of the distinct part of the example query, as this is implicit by using the group by and serves no purpose:
select id, max(value) value
from
(select '1' id,'br' value
union
select '1','er'
union
select '2','rt'
union
select '2','uj'
union
select '3','iu'
union
select '3','ol'
union
select '3','ol') data
group by id
Output:
idvalue
1er
2uj
3ol
July 15, 2008 at 12:12 pm
Heh... you guys are missing the real problem! 😉 The real problem is why does the OP NOT care about which data is returned? If you don't care, then it shouldn't be returned to begin with because it doesn't mean anything real. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
July 15, 2008 at 1:15 pm
Maybe it's just a starting condition?
"Get these records and update a flag".
Call a SP to process the previously marked records.
Now start from beginning by picking a random record for each group.
"Get these records and update a flag".
Call a SP to process the previously marked records.
Now start from beginning by picking a random record for each group.
...
Until there are no more records?
N 56°04'39.16"
E 12°55'05.25"
July 15, 2008 at 10:50 pm
Jeff Moden (7/15/2008)
Heh... you guys are missing the real problem! 😉 The real problem is why does the OP NOT care about which data is returned? If you don't care, then it shouldn't be returned to begin with because it doesn't mean anything real. 😉
hehe! 🙂
that brings me to my next question - how would i get the 2nd / 3rd /4th ..... element for any particular id..
i was wondering if i cud have another column which wud tell whether its the 0th, 1st, 2nd.... element for that id ?
1 br 0
1 er 1
2 rt 0
3 yh 0
2 uj 1
3 iu 1
3 ol 2
July 15, 2008 at 11:39 pm
There are a couple of ways... I believe the fastest for large data sets in SQL Server 2000 would be in the following...
http://www.sqlservercentral.com/articles/Advanced+Querying/61716/
--Jeff Moden
Change is inevitable... Change for the better is not.
July 16, 2008 at 3:12 am
Select distinct id, Name = ''
into #t1
from Data
update #t1
set Name = Name
from Data
where #t1.id = Data.id
karthik
July 16, 2008 at 6:45 am
Hi Jeff!
Your code here just selecting and ordering asc the data inside the table it doesn't do any elimination of duplicate records!
Can you explain little bit this!
Dugi
July 16, 2008 at 6:55 am
well - try this
create table test99
(seq int, strin varchar(2))
insert into test99
values (3, 'ol')
insert into test99
values (3, 'iu')
select distinct z.seq,
(select top 1 strin
from test99 x
where x.seq = z.seq)
from test99 z
Viewing 15 posts - 1 through 15 (of 58 total)
You must be logged in to reply to this topic. Login to reply