Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Over Partition with multiple duplicate identifying columns

$
0
0

Greetings all,

I'm facing a rather difficult situation with an over partition piece of code I wrote to remove duplicate records from a table. Basically, we have a table named "Record" which contains technical information for our customers' installations. We also have a "Case" table which contains account information. The "Record" table links to the "Case" table by way of RecordID (which is referred to Idkey in the "Record" table itself but as RecordID in the "Case" table). Now, I'm using the "installation" column in the "Record" table to identify duplicate records. So far so good, but here's the catch: A record is only considered as a duplicate IF the installation number appears more than once in the "Record" table AND a column named "Showit" in the "Case" table which is a bit appears as true for more than one record. In other words, if I have installation number 11458745 three times in the Record table, but out of those three records, only one has the Showit bit as true in the "Case" table, it isn't considered a duplicate. Now if a an installation number appeared more than once in the "Record" table AND more than one of those records had the "Showit" bit set to true in the Case table, then we'd have duplicates and I'd have to keep only one. At this point, I have a piece of code written as a select statement with an over partition to allow me to visualize which records are duplicated based on just the installation number appearing more than once, but it doesn't take the "Showit" column in the Case table into consideration at all. All I'm looking to achieve right now, is to be able to visualize within this select statement only installation numbers that appear more than once in the "Record" table, but also have more than one "Showit" set to true in the "Case" table. Any feedback would be greatly appreciated. Below is my query:



SELECT 
--r.IdKey AS RecordID
r.Installation
, COUNT(Installation) AS NumOFDupes
FROM
( SELECT IdKey  -- PK
, Installation  -- Duplicate Identifier
, CreatedOn
, ROW_NUMBER() OVER(PARTITION BY Installation ORDER BY Installation DESC) AS rn
  FROM 
Data.tblRecord ) r
INNER JOIN Case (NOLOCK) c ON r.Idkey = c.RecordID AND CAST(r.CreatedON AS DATE) ! = '2012-11-09'
INNER JOIN Tx (NOLOCK) t ON c.Idkey = t.CaseID AND t.MeterNumber ! = 0
WHERE 
r.rn ! = 1
AND r.Installation IS NOT NULL
GROUP BY 
Installation
ORDER BY
NumOFDupes DESC


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>