May 6, 2008 at 11:29 pm
[font="Courier New"]DECLARE @TABLE TABLE
(
EmpID CHAR(3)
,Category CHAR(1)
)
INSERT INTO @TABLE VALUES('101','A')
INSERT INTO @TABLE VALUES('102','B')
INSERT INTO @TABLE VALUES('103','C')
INSERT INTO @TABLE VALUES('104','A')
INSERT INTO @TABLE VALUES('105','B')
INSERT INTO @TABLE VALUES('106','C')
INSERT INTO @TABLE VALUES('107','D')
INSERT INTO @TABLE VALUES('108','A')
INSERT INTO @TABLE VALUES('109','A')
INSERT INTO @TABLE VALUES('110','D')[/font]
[font="Tahoma"]I need to remove record from each duplicated values in [Category] field.
Ex : One record from each A,B,C and D. Any Idea?[/font]
- Zahran -
May 6, 2008 at 11:35 pm
Let's talk about just the ones for Category "A"... which one do you want to keep? The one with the lowest value EmpID or the highest?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 6, 2008 at 11:44 pm
[font="Tahoma"]I want to remove the lowest value in each category[/font]
- Zahran -
May 6, 2008 at 11:53 pm
I use following query to remove those records. Problem solved.
[font="Courier New"]DELETE FROM @TABLE
WHERE EmpID IN(SELECT MIN(EmpID) AS EmpID FROM @TABLE GROUP BY Category)[/font]
- Zahran -
May 7, 2008 at 7:50 am
You do realize that code will remove all single rows, correct?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 8, 2008 at 11:37 am
delete @Table
from @Table t
left join (
select Max(EmpID) as MaxID, Category
from @Table
group by Category
) t2
on t.EmpID = t2.MaxID
where t2.MaxId is null;
-- Check results
select *
from @Table;
Tomm Carr
--
Version Normal Form -- http://groups.google.com/group/vrdbms
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply