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 2005 Forums
 Transact-SQL (2005)
 additional filter
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

magmo
Aged Yak Warrior

533 Posts

Posted - 01/10/2013 :  04:23:01  Show Profile  Reply with Quote
Hi

I have the following code....



UPDATE t
SET t.Duplicate = CASE WHEN ISNULL(AppTimeStamp,'') ='' OR Rn=1 THEN 0 ELSE 1 END

FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY AppTimeStamp ORDER BY ID DESC) AS rn,AppTimeStamp,Duplicate 
FROM tbl_Cards WHERE (Isfetched IS NULL OR Isfetched = 0) AND (Duplicate IS NULL)
)t



This code search the database for duplicate records based on a column called AppTimeStamp, if a duplicate is found its marked as duplicate. But I need to add another condition, Both AppTimeStamp and another column called AppID should match, if both those values are the same, then it should be marked as duplicate.

I'm not sure on how to do that and could use some help.

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  04:30:14  Show Profile  Reply with Quote
assuming AppID is also in same table, you can do like this

UPDATE t
SET t.Duplicate = CASE WHEN ISNULL(AppTimeStamp,'') ='' OR Rn=1 OR AppID != AppTimeStamp THEN 0 ELSE 1 END

FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY AppTimeStamp ORDER BY ID DESC) AS rn,AppTimeStamp,Duplicate,AppID 
FROM tbl_Cards WHERE (Isfetched IS NULL OR Isfetched = 0) AND (Duplicate IS NULL)
)t


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


Edited by - visakh16 on 01/10/2013 04:30:33
Go to Top of Page

magmo
Aged Yak Warrior

533 Posts

Posted - 01/10/2013 :  06:02:50  Show Profile  Reply with Quote
Hi

Sorry, I meant that it should look for duplicates where both the AppTimeStamp and AppID should be the same.

For example

ID | AppID | AppTimeStamp | IsFetched
1 11 123
2 11 123
3 12 123
4 23 456

In this example ID 1 and 2 is identical and id 1 should be marked as duplicate, but ther other id's should not be marked as duplicates.


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  06:10:45  Show Profile  Reply with Quote

UPDATE t
SET t.Duplicate = CASE WHEN ISNULL(AppTimeStamp,'') ='' OR Rn=1 THEN 0 ELSE 1 END

FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY AppID,AppTimeStamp ORDER BY ID DESC) AS rn,AppTimeStamp,Duplicate ,COUNT(1) OVER (PARTITION BY AppID, AppTimeStamp ) AS Occ
FROM tbl_Cards WHERE (Isfetched IS NULL OR Isfetched = 0) AND (Duplicate IS NULL)
)t
WHERE Occ >1



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

Edited by - visakh16 on 01/10/2013 06:11:09
Go to Top of Page

magmo
Aged Yak Warrior

533 Posts

Posted - 01/10/2013 :  06:19:47  Show Profile  Reply with Quote
When I run that it doesnt mark any row at all...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  06:27:24  Show Profile  Reply with Quote
quote:
Originally posted by magmo

When I run that it doesnt mark any row at all...


that may be because of your other conditions

ie ISNULL(AppTimeStamp,'') ='' OR Rn=1

i dont know whats significance of them though

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

Go to Top of Page

magmo
Aged Yak Warrior

533 Posts

Posted - 01/10/2013 :  10:24:56  Show Profile  Reply with Quote
Its not beacuse of AppTimeStamp (ISNULL(AppTimeStamp,'') ='') since that value is not empty in my table...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  23:22:26  Show Profile  Reply with Quote
show sample data and explain what exactly you're trying to achieve

------------------------------------------------------------------------------------------------------
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.08 seconds. Powered By: Snitz Forums 2000