| Author |
Topic  |
|
|
magmo
Constraint Violating Yak Guru
465 Posts |
Posted - 01/10/2013 : 04:23:01
|
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
47099 Posts |
Posted - 01/10/2013 : 04:30:14
|
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 |
 |
|
|
magmo
Constraint Violating Yak Guru
465 Posts |
Posted - 01/10/2013 : 06:02:50
|
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.
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47099 Posts |
Posted - 01/10/2013 : 06:10:45
|
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 |
 |
|
|
magmo
Constraint Violating Yak Guru
465 Posts |
Posted - 01/10/2013 : 06:19:47
|
| When I run that it doesnt mark any row at all... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47099 Posts |
Posted - 01/10/2013 : 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 conditions
ie ISNULL(AppTimeStamp,'') ='' OR Rn=1
i dont know whats significance of them though
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
magmo
Constraint Violating Yak Guru
465 Posts |
Posted - 01/10/2013 : 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
India
47099 Posts |
Posted - 01/10/2013 : 23:22:26
|
show sample data and explain what exactly you're trying to achieve
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|