Author |
Topic |
magmo
Aged Yak Warrior
558 Posts |
Posted - 2013-01-10 : 04:23:01
|
Hi I have the following code....UPDATE tSET t.Duplicate = CASE WHEN ISNULL(AppTimeStamp,'') ='' OR Rn=1 THEN 0 ELSE 1 ENDFROM(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
52326 Posts |
Posted - 2013-01-10 : 04:30:14
|
assuming AppID is also in same table, you can do like thisUPDATE tSET t.Duplicate = CASE WHEN ISNULL(AppTimeStamp,'') ='' OR Rn=1 OR AppID != AppTimeStamp THEN 0 ELSE 1 ENDFROM(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 MVPhttp://visakhm.blogspot.com/ |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2013-01-10 : 06:02:50
|
HiSorry, I meant that it should look for duplicates where both the AppTimeStamp and AppID should be the same.For exampleID | AppID | AppTimeStamp | IsFetched1 11 1232 11 1233 12 1234 23 456In 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. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-10 : 06:10:45
|
[code]UPDATE tSET t.Duplicate = CASE WHEN ISNULL(AppTimeStamp,'') ='' OR Rn=1 THEN 0 ELSE 1 ENDFROM(SELECT ROW_NUMBER() OVER (PARTITION BY AppID,AppTimeStamp ORDER BY ID DESC) AS rn,AppTimeStamp,Duplicate ,COUNT(1) OVER (PARTITION BY AppID, AppTimeStamp ) AS OccFROM tbl_Cards WHERE (Isfetched IS NULL OR Isfetched = 0) AND (Duplicate IS NULL))tWHERE Occ >1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2013-01-10 : 06:19:47
|
When I run that it doesnt mark any row at all... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-10 : 06:27:24
|
quote: Originally posted by magmo When I run that it doesnt mark any row at all...
that may be because of your other conditionsie ISNULL(AppTimeStamp,'') ='' OR Rn=1i dont know whats significance of them though------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2013-01-10 : 10:24:56
|
Its not beacuse of AppTimeStamp (ISNULL(AppTimeStamp,'') ='') since that value is not empty in my table... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-10 : 23:22:26
|
show sample data and explain what exactly you're trying to achieve------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|