Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 additional filter

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2013-01-10 : 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

52326 Posts

Posted - 2013-01-10 : 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/

Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2013-01-10 : 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.


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-10 : 06:10:45
[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 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
[/code]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

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

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 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

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...
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -