SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Help with duplicates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cwizards
Starting Member

United Kingdom
1 Posts

Posted - 02/04/2013 :  09:53:58  Show Profile  Reply with Quote
Hi, I have a table with four fields (that are of interest), id, refnumber, dateadded and canceled. Previously the code selected the latest date (dateadded) and the refnumber to get the record required as there were duplicate refnumber records. As my new code utilises a table join method, I need to mark the old records as canceled (=1) leaving the most recent uncanceled (=0).
example
1 48394A0021 2002-01-01 0
2 54739A0040 2002-01-01 0
3 54739A0150 2002-01-01 0
4 54739A0150 2004-01-01 1
ids 3&4 are duplicate ref numbers and I need to set the canceled as shown (0 for the older one(s) and 1 for the latest)
Can this be done in a SQL statement?
Your help will be gratefully received
Nick

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/04/2013 :  12:27:49  Show Profile  Reply with Quote

UPDATE t
SET cancelled = CASE WHEN Seq=1 THEN 1 ELSE 0 END
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY refnumber ORDER BY dateadded DESC) AS seq,cancelled
FROM table
)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 02/05/2013 :  02:41:34  Show Profile  Reply with Quote
ROW_NUMBER() is not available in MS SQL 2000.
So this is the alternative solution
I assumed that if there is single refnumber then that has to be marked as 1 ( i.e. RecentDate)

DECLARE @example TABLE(refnumber VARCHAR(20), dateadded date, cancelled BIT)
INSERT INTO @example
SELECT '48394A0021', '2002-01-01', 0 union all  -- Single refnumber
SELECT '54739A0040', '2002-01-01', 0 union all  -- Single refnumber
SELECT '54739A0150', '2002-01-01', 0 union all
SELECT '54739A0150', '2004-01-01', 1            -- marked as 1 b'coz latest record for refnumber '54739A0150'
UPDATE @example SET
cancelled = 1 
FROM @example e 
JOIN (SELECT refnumber, MAX(dateadded) RecentDate
		FROM @example
		GROUP BY refnumber
	 ) t
ON e.refnumber= t.refnumber AND e.dateadded= t.RecentDate


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/05/2013 :  02:46:59  Show Profile  Reply with Quote
didnt notice this is 2000 forum
Anyways, it should be this unless you've default constraint on cancelled field to make it 0

UPDATE @example SET
cancelled = CASE WHEN t.refnumber IS NOT NULL THEN  1 ELSE 0 END
FROM @example e 
LEFT JOIN (SELECT refnumber, MAX(dateadded) RecentDate
		FROM @example
		GROUP BY refnumber
	 ) t
ON e.refnumber= t.refnumber AND e.dateadded= t.RecentDate


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000