Combined Item number and serial number to be unique value

I have a report that sends out a DB mail alert when duplicate serial numbers are detected across company databases, and it works fine 99 present of the time, however we have had a couple of situations where it detects a duplicate serial number condition, but in those cases they weren't actually duplicates as they had the same serial number but different Item numbers. My code is looking at strictly serial numbers but apparently that is not 100 Percent accurate, so what I would like to do is ignore the results if the serial number is the same but the Item number is different.

This is the view (SSG_AD_AllSerials) that is called by the view that generates the report
SELECT        SERLNMBR, ITEMNMBR, LOCNCODE, DEX_ROW_ID, 'AMCHR' AS Company
FROM            dbo.IV00200
WHERE        SERLNMBR <> 'NONE'
UNION ALL
SELECT        SERLNMBR, ITEMNMBR, LOCNCODE, DEX_ROW_ID, 'ATI' AS Company
FROM            ATI.dbo.IV00200
WHERE        SERLNMBR <> 'NONE'
UNION ALL
SELECT        SERLNMBR, ITEMNMBR, LOCNCODE, DEX_ROW_ID, 'ATIMF' AS Company
FROM            ATIMF.dbo.IV00200
WHERE        SERLNMBR <> 'NONE'

Open in new window

   

View that generates the report
SELECT        TOP (100) PERCENT SERLNMBR AS Serial_No, ITEMNMBR AS Item_No, LOCNCODE AS Site, Company
FROM            dbo.SSG_AD_AllSerials
WHERE        (SERLNMBR IN
                             (SELECT        SERLNMBR
                               FROM            dbo.SSG_AD_AllSerials AS SSG_AD_AllSerials_1
                               GROUP BY SERLNMBR
                               HAVING         (COUNT(*) <> 1))) 
ORDER BY Serial_No

Open in new window

skull52IT director Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
A simple concatenation should do it for you:

SELECT        TOP (100) PERCENT SERLNMBR AS Serial_No, ITEMNMBR AS Item_No, LOCNCODE AS Site, Company
FROM            dbo.SSG_AD_AllSerials
WHERE        (SERLNMBR + '-' + ITEMNMBR IN
                             (SELECT        SERLNMBR + '-' + ITEMNMBR 
                               FROM            dbo.SSG_AD_AllSerials AS SSG_AD_AllSerials_1
                               GROUP BY SERLNMBR + '-' + ITEMNMBR 
                               HAVING         (COUNT(*) <> 1))) 
ORDER BY Serial_No

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
Concatenation might hurt performance.  You could also join to the dups:


SELECT        ssa.SERLNMBR AS Serial_No, ssa.ITEMNMBR AS Item_No, ssa.LOCNCODE AS Site, ssa.Company
FROM            dbo.SSG_AD_AllSerials saa
INNER JOIN (
                 SELECT        SERLNMBR, ITEMNMBR
                 FROM            dbo.SSG_AD_AllSerials AS SSG_AD_AllSerials_1
                 GROUP BY  SERLNMBR, ITEMNMBR
                 HAVING        (COUNT(*) > 1)
           ) AS dup_serials ON dup_serials.SERLNMBR = saa.SERLNUMBR AND dup_serials.ITEMNMBR = saa.ITEMNMBR
                 
ORDER BY ssa.Serial_No
0
skull52IT director Author Commented:
Scott,
that generates the following errors

Msg 207, Level 16, State 1, Line 8
Invalid column name 'SERLNUMBR'.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "ssa.SERLNMBR" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "ssa.ITEMNMBR" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "ssa.LOCNCODE" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "ssa.Company" could not be bound.
Msg 4104, Level 16, State 1, Line 10
The multi-part identifier "ssa.Serial_No" could not be bound.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

skull52IT director Author Commented:
Scott,
Also performance is not really an issues as this is part of a SQL job that runs in the middle of the night when no one is on the system.
0
Scott PletcherSenior DBACommented:
Should be 'saa.' instead of 'ssa.'
0
skull52IT director Author Commented:
Yeah I missed that one...
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
skull52, do you still need help with this question?
0
skull52IT director Author Commented:
Thanks to all for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.