Selecting specific rows from ROW_NUMBER result set

Following is a result of a query using ROW_NUMBER() with partition

Assuming that this result is stored in a table variable 'RESULT',, I want to select only the barcodes with one row. All the barcodes with maximum ROWNUM >1 should be ignored. In the example i gave,  row with id 4 only should be returned.
Can anyone help?
Who is Participating?

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

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.

Daniel Van Der WerkenIndependent ConsultantCommented:
Do a NOT IN and then subquery. For example:

FROM @Result V
   SELECT T.BarCode
   FROM @Result T
   GROUP BY T.BarCode
   HAVING COUNT(T.Barcode) > 1

Open in new window

That should do it. If not, because I didn't actually test this, then you could create a second table variable with my subquery but it would have this:

DECLARE @ResultNotIn TABLE (Barcode VARCHAR(100), CountBarcode INT)
   SELECT T.BarCode, COUNT(T.BarCode)
   FROM @Result T
   GROUP BY T.BarCode
   HAVING COUNT(T.Barcode) > 1

SELECT * FROM @Result WHERE Barcode NOT IN (SELECT BarCode FROM @ResultNotIN)

Open in new window

That would work but be cludgier. If Cludgier is a word.
PortletPaulEE Topic AdvisorCommented:
is it Kludgier?

anyway, perhaps you should go back one step, to where you use the row_number

also use COUNT(*) OVER(partition by ...)  using the same partition as you do for rownum


id, barcode, row_number() over(partition by barcode order by (select null)) as rownum
, count(*) over(partition by barcode) as countnum
from soucetable

Then you can select the barcodes with only a single row using


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
mvcdeveloperdxbAuthor Commented:
Great!! This worked very well
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.