简体   繁体   中英

SQL Server query - get items that exist in more than one column

I have a simple table which contains barcode ids of tools and associated room location in which the tool should belong to.

Unfortunately, I've noticed that some users have entered the same barcode id for another room location.

For example, I have these 2 columns:

barcodeNumber | RoomLocation
--------------+-------------
    123456    |    400
    654321    |    300
    875421    |    200
    654321    |    400
    999999    |    250
    878787    |    300
    777777    |    400
    999999    |    200

Note that barcodeNumber "654321" is stored in roomLocations 300 & 400 ad "999999" are stored in room locations 200 & 250

How do I write the SQL query to list the duplicate barcode Number and RoomLocation they are located in and not just the "count" of duplicates?

For example, the end result I wish to see is:

654321 | 300
654321 | 400
999999 | 200
999999 | 250

Using window functions (SQL:1999) you would get the result like this:

with c as (
select barcodeNumber, RoomLocation,
count(*) over(partition by barcodeNumber) cnt
from t)
select barcodeNumber, RoomLocation 
from c where cnt > 1
order by 1,2

You can also use SQL-92 syntax:

select barcodeNumber, RoomLocation
from t
where barcodeNumber IN (
  select barcodeNumber from t
  group by barcodeNumber
  having count(*) > 1)
order by 1,2

You can try this also. Use count(*) over (partition by barcodenumber) to determine the duplicate values.

create table #sample (barcodenumber nvarchar(30),roomlocation int)
insert into #sample (barcodenumber,roomlocation)
select '123456',400 union all
select '654321',300 union all
select '875421',200 union all
select '654321',400 union all
select '999999',250 union all
select '878787',300 union all
select '777777',400 union all
select '999999',200

select  barcodenumber,roomlocation  from (
 select *, count(*) over (partition by barcodenumber) as rnk
 from #sample
 )t
 group by barcodenumber,roomlocation,rnk
 having rnk >1

Hope this could help.

Do you want to find the duplicate barcode?

;WITH tb(barcodenumber,roomlocation)AS(
    SELECT '123456',400 UNION ALL
    SELECT '654321',300 UNION ALL
    SELECT '875421',200 UNION ALL
    SELECT '654321',400 UNION ALL
    SELECT '999999',250 UNION ALL
    SELECT '878787',300 UNION ALL
    SELECT '777777',400 UNION ALL
    SELECT '999999',200
)
SELECT * FROM (
    SELECT *,COUNT(0)OVER(PARTITION BY tb.barcodenumber) AS cnt FROM tb
) AS t WHERE t.cnt>1
barcodenumber roomlocation cnt
------------- ------------ -----------
654321        400          2
654321        300          2
999999        200          2
999999        250          2

Here is another way to achive your result:

SELECT barcodenumber, roomlocation
FROM table_name
WHERE barcodenumber IN (
        SELECT barcodenumber
        FROM table_name
        GROUP BY barcodenumber
        HAVING COUNT(DISTINCT roomlocation) > 1);
        --If you dont have duplicate rows then just use COUNT(*)

Use JOIN and HAVING clause :

SELECT A.barcodenumber,roomlocation
FROM #sample
JOIN 
(
  SELECT barcodenumber
  FROM #sample
  GROUP BY barcodenumber
  HAVING COUNT(*) > 1
) A ON A.barcodenumber = #sample.barcodenumber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM