编号与序号重复

时间:2015-04-30 07:25:12

标签: sql sql-server ms-access

这是MS Access还是SQL Server中可能出现的问题?我使用GROUP BY子句以不同的方式尝试了它。

我想找出重复项(连接销售订单和项目)和序号。列'计划'是我正在寻找的。

duplicates numbering

2 个答案:

答案 0 :(得分:0)

使用SQL CTE表达式获取concat重复值

让我们将您的表名保持为销售,将列名保持为销售订单,item,concat

With CTE(Duplicates,item,concat)
as
(
SELECT ROW_NUMBER() OVER(PARTITION by salesorder,item ORDER BY salesorder )
AS duplicates,item, salesorder+''+item
FROM sales 
)
--Now Delete Duplicate Records
select * FROM CTE
WHERE duplicates > 1

试试这个它会起作用。

答案 1 :(得分:0)

试试这个样本数据。您将获得所需的确切答案..

Begin Tran
create table sales
(
salesorder varchar(15),
item varchar(5)
)

insert into sales values('3040117007',1)
 insert into sales values('3040117007',1)
  insert into sales values('3040117007',1)



insert into sales values('3040179299',1)
 insert into sales values('3040179299',1)
  insert into sales values('3040182496',1)
  insert into sales values('3040182496',1)
  insert into sales values('3040182496',1)
  insert into sales values('3040182496',1)

select * from sales


;With CTE(Schedule,salesorder,item,concats)
as
(
SELECT ROW_NUMBER() OVER(PARTITION by salesorder,item ORDER BY salesorder )
AS duplicates,salesorder,item,salesorder+''+item
FROM sales
)
--Now get Duplicate Records
select 'Duplicate_Records'* FROM CTE
WHERE Schedule > 1

rollback tran

替换此查询==> "选择' Duplicate_Records' *来自CTE     时间表> 1"

选择" All_Records' *来自CTE"获取所有记录