Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server查询-获取存在于多个列中的项_Sql_Sql Server - Fatal编程技术网

SQL Server查询-获取存在于多个列中的项

SQL Server查询-获取存在于多个列中的项,sql,sql-server,Sql,Sql Server,我有一个简单的表格,其中包含工具的条形码ID和工具应属于的相关房间位置 不幸的是,我注意到一些用户为另一个房间位置输入了相同的条形码id 例如,我有以下两列: barcodeNumber | RoomLocation --------------+------------- 123456 | 400 654321 | 300 875421 | 200 654321 | 400 999999 | 2

我有一个简单的表格,其中包含工具的条形码ID和工具应属于的相关房间位置

不幸的是,我注意到一些用户为另一个房间位置输入了相同的条形码id

例如,我有以下两列:

barcodeNumber | RoomLocation
--------------+-------------
    123456    |    400
    654321    |    300
    875421    |    200
    654321    |    400
    999999    |    250
    878787    |    300
    777777    |    400
    999999    |    200
请注意,条形码“654321”存储在房间位置300和400,而“999999”存储在房间位置200和250

如何编写SQL查询以列出重复的条形码编号和它们所在的房间位置,而不仅仅是重复的“计数”

例如,我希望看到的最终结果是:

654321 | 300
654321 | 400
999999 | 200
999999 | 250
使用窗口函数(SQL:1999)可以得到如下结果:

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
您还可以使用SQL-92语法:

select barcodeNumber, RoomLocation
from t
where barcodeNumber IN (
  select barcodeNumber from t
  group by barcodeNumber
  having count(*) > 1)
order by 1,2
使用窗口函数(SQL:1999)可以得到如下结果:

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
您还可以使用SQL-92语法:

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

你也可以试试这个。在(按条形码编号划分)上使用
count(*)
确定重复值

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

希望这能有所帮助。

你也可以试试这个。在(按条形码编号划分)上使用
count(*)
确定重复值

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

希望这能有所帮助。

是否要查找重复的条形码

;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
条码号房间位置cnt ------------- ------------ ----------- 654321 400 2 654321 300 2 999999 200 2 999999 250 2
是否要查找重复的条形码

;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
条码号房间位置cnt ------------- ------------ ----------- 654321 400 2 654321 300 2 999999 200 2 999999 250 2
以下是另一种获得结果的方法:

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(*)

以下是另一种获得结果的方法:

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(*)

使用JOIN和HAVING子句:

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

使用JOIN和HAVING子句:

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

谢谢你,曼苏尔谢谢你,曼苏尔