December 2, 2010 at 6:42 am
Hi guys,
Can you offer advice on how to extract duplicates from a specific number range - please see example below:
SELECT Number, Firstname, Lastname, Email from dbo.Person
WHERE Number BETWEEN '10000' AND '11000' or
Number BETWEEN '13000' AND '14000' or
Number BETWEEN '18000' AND '19000' or
Number BETWEEN '20000' AND '23000' or
Number = '25141' or Number = '25144' or Number = '26221' or Number = '27889' or Number = '27899' or Number = '28888'
ORDER BY Number ASC
I'm unsure how to convert the below to suit my needs?
SELECT Extension
FROM dbo.Person
WHERE ...
GROUP BY Extension
HAVING ( COUNT(Extension) = 1 )
Many thanks
December 2, 2010 at 6:49 am
Hello Lotz,
You can use Group By with Count(*) > 1 condition
Here is a script
SELECT
Number, Firstname, Lastname, Email
from dbo.Person
WHERE
Number BETWEEN '10000' AND '11000' or
Number BETWEEN '13000' AND '14000' or
Number BETWEEN '18000' AND '19000' or
Number BETWEEN '20000' AND '23000' or
Number = '25141' or Number = '25144' or Number = '26221' or Number = '27889' or Number = '27899' or Number = '28888'
Group by Number, Firstname, Lastname, Email
having COUNT(*) > 1
And also you can check articles delete duplicate records or ROW_NUMBER() OVER (PARTITION BY for removing duplicates
December 2, 2010 at 7:01 am
You might find it easier to use a temp table to hold your ranges. Finding the dupes is straightforward:
DROP TABLE #Person
CREATE TABLE #Person (Number CHAR(5), Firstname VARCHAR(20), Lastname VARCHAR(40), Email VARCHAR(50))
INSERT INTO #Person (Number, Firstname, Lastname, Email)
SELECT '18000', 'John', 'Smith', '' UNION ALL
SELECT '18000', 'Joe', 'Jones', '' UNION ALL
SELECT '13000', 'Mary', 'White', '' UNION ALL
SELECT '27889', 'Fred', 'Armstrong', '' UNION ALL
SELECT '23000', 'Steve', 'Carter', '' UNION ALL
SELECT '23000', 'Karthik', 'K', ''
DROP TABLE #NumberRanges
CREATE TABLE #NumberRanges (RangeStart CHAR(5), RangeEnd CHAR(5))
INSERT INTO #NumberRanges (RangeStart, RangeEnd)
SELECT '10000', '11000' UNION ALL
SELECT '13000', '14000' UNION ALL
SELECT '18000', '19000' UNION ALL
SELECT '20000', '23000'
;WITH DupeDetector AS (
SELECT Number, Firstname, Lastname, Email,
Instances = COUNT(*) OVER(PARTITION BY Number)
FROM #Person p
INNER JOIN #NumberRanges r ON p.number BETWEEN r.RangeStart AND r.RangeEnd
) SELECT * FROM DupeDetector WHERE Instances > 1
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 2, 2010 at 8:35 am
Eralper - a slight amendment to your code done the trick.
Many thanks for the responses guys.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply