sqlserver去重复,sqlserver去重复 保留一条

sqlserver利用存储过程去除重复行的sql语句

还是先上代码吧

创新互联主要从事网站设计、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务遂宁,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

,可以先看

SQL语句去掉重复记录,获取重复记录

复制代码

代码如下:

ALTER

procedure

[dbo].[PROC_ITEMMASTER_GETUNIQUE]

@PAGEINDEX

INT,@uid

int,@itemnumber

varchar(50)

AS

begin

tran

--开始事务

drop

table

[ItemMaster].[dbo].[testim]

--删除表

--把不重复记录转存到testim中

select

*

into

[ItemMaster].[dbo].[testim]

from

[ItemMaster].[dbo].[dat_item_master]

where

item_uid

in(select

min(item_uid)

as

item_uid

from

[ItemMaster].[dbo].[dat_item_master]

group

by

item_number)

and

status=0

select

top

10

*

from

[ItemMaster].[dbo].[testim]

where

item_uid

not

in

(select

top

(10*(@PAGEINDEX-1))

item_uid

from

[ItemMaster].[dbo].[testim])

and

owneruid=@uid

and

item_number

like

@itemnumber+'%'

--判断是否出错

if

@@error0

begin

rollback

tran

--出错则回滚

end

else

begin

--否则提前事务

commit

tran

end

我的数据是这样的:因为item_uid是标识列,item_number有重复的,

我想过滤成这样:

顺带说几个在编程的时候遇到的小问题

1.程序

出现

Could

not

find

stored

procedure

找不到这个存储过程

因为我的程序数据库有四个,而默认连接是A,但实际要执行B库里的存储过程,导致出错,

解决办法1:可在A里面建个一样的存储过程2:在执行连接的时候,替换下数据库就行了

2.

asp.net/C#

将存储过程中返回的数据集,填充到dataset/datatable

复制代码

代码如下:

SqlConnection

conn

=

new

SqlConnection(ConfigurationManager.ConnectionStrings["SolutionSQLServer"].ToString());

SqlCommand

cmd

=

new

SqlCommand("Test",conn);

cmd.CommandType

=

CommandType.StoredProcedure;

cmd.Parameters.Add("@MaxId",

SqlDbType.Int).Value

=

12000;

SqlDataAdapter

sda

=

new

SqlDataAdapter(cmd);

DataTable

dt

=

new

DataTable();

sda.Fill(dt);

在这感谢

3.在存储过程里面,写SQL语句不能动态不加order

by

功能

比如

复制代码

代码如下:

--·@new_orderby

是传入参数,不能这样写

select

top

(10*(2-1))

item_uid

from

testim

order

by

@new_orderby

--执行这个的时候,SQL会出现

The

SELECT

item

identified

by

the

ORDER

BY

number

1

contains

a

variable

as

part

of

the

expression

identifying

a

column

position.

Variables

are

only

allowed

when

ordering

by

an

expression

referencing

a

column

name.

不过我找到解决办法,不过很麻烦,

(第二个回答用

'

sql

'进行连接)

(用case

end

也行)

4.

select

into

insert

into

select

两种复制文句

(这里感谢)

1.INSERT

INTO

SELECT语句

语句形式为:Insert

into

Table2(field1,field2,...)

select

value1,value2,...

from

Table1

要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。

2.SELECT

INTO

FROM语句

语句形式为:SELECT

vale1,

value2

into

Table2

from

Table1

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。

5.顺便复习下常用的SQL方法语句

复制代码

代码如下:

declare

@name

varchar(200)

--声明变量

set

@name='abcd;def'

--赋值

print

'exec

len

:'+Convert(varchar(10),Len(@name))

--convert(type,value)转换,Len(value)获取大小

print

'exec

charindex:'+Convert(varchar(10),CharIndex('e',@name))--CharIndex(find,value)

在value中查找find的位置

print

'not

replace:'+@name

print

'exec

replace:'+Replace(@name,';','')

--用replace替换

print

'exec

substring:'+Substring(@name,0,3)--用substring截取

print

@@RowCount

--返回上一行代码受影响的行数

作者:chenhuzi

sqlserver怎么删除重复数据

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

select

* from people

where peopleId in (select peopleId from

people group by peopleId having count(peopleId)

1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

delete

from people

where peopleId in (select peopleId from

people group by peopleId having

count(peopleId) 1)

and rowid not in (select min(rowid) from

people group by peopleId having count(peopleId

)1)

3、查找表中多余的重复记录(多个字段)

select * from vitae a

where (a.peopleId,a.seq)

in (select peopleId,seq from vitae group by peopleId,seq having

count(*) 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

delete from vitae a

where

(a.peopleId,a.seq) in (select peopleId,seq from vitae group by

peopleId,seq having count(*) 1)

and rowid not in (select min(rowid) from

vitae group by peopleId,seq having count(*)1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

select * from vitae a

where

(a.peopleId,a.seq) in (select peopleId,seq from vitae group by

peopleId,seq having count(*) 1)

and rowid not in (select min(rowid) from

vitae group by peopleId,seq having count(*)1)

(二)

比方说

在A表中存在一个字段“name”,

而且不同记录之间的“name”值有可能会相同,

现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;

Select

Name,Count(*) From A Group By Name Having Count(*) 1

如果还查性别也相同大则如下:

Select Name,sex,Count(*) From A Group By Name,sex Having

Count(*) 1

mysql,sqlserver数据库去重

b. 方法:

☆根据dname分组,查找出deptno最小的。然后再查找deptno不包含刚才查出来的。这样就查询出了所有的重复数据(除了deptno最小的那行)

方法2

删除重复的行

单个字段的如果会了,多个字段也非常简单。就是将group by 的字段增加为你想要的即可。

此处只写一个,其他方法请仿照一个字段的写即可。

查询结果不含指定字段重复

2.表需要删除重复的记录(重复记录保留1条),

3.查询重复

4.1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

4.2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

4.3、查找表中多余的重复记录(多个字段)

4.4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

4.5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

4.6.消除一个字段的左边的第一位:

4.7.消除一个字段的右边的第一位:

4.8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录

查询重复

sqlserver 数据有重复怎么删除

1、必须保证表中有主键或者唯一索引,或者某列数据不能重复。只有这样,才可能使用一句SQL来实现。否则只能考虑其它办法。下面的语句,假定BB列是不重复的,删除后保存BB列值最大的那条记录。

delete

from

where

aa

in

(select

aa

from

group

by

aa

having

count(aa)

1)

and

bb

not

in

(select

max(bb)

from

group

by

aa

having

count(aa)

1);

2、有多种写法:

delete

A

from

B

where

A.AA

=

B.AA

delete

A

from

A,B

where

A.AA

=

B.AA

delete

A

where

AA

in

(select

AA

from

B)

3、使用into关键字:

select

*

into

新表名

from

原表

4、取数据前3位,字段必须是类似char类型,使用类似substring这样的函数(SYBASE是substring,ORACLE是substr):

select

substring(字段,1,3)

from

表名

SQLServer去重复查询,不删除重复数据

1、要有定位基准,也就是说,你的表必需要有一个不重复的键值,如果没有,请你给这个表加一个字段,将这个字段设为自增变量字段,建议为int类型,比如字段名可为“编码”。

2、查重复的数据:

select *from 表名 where 编码 in

(select 编码 from 表名 group by 编码 having count(1) = 2)

3、删除所有有重复的记录:

delete from 表名 where 

编码 in(select 编码 from 表名 group by 编码 having count(1) = 2)

4、删去重复的,只留下重复记录中编码最大的一条:

delete from 表名 where 

编码 in(select 编码 from 表名 group by 编码 having count(1) = 2) 

and 编码 not in (select max(编码)from 表名 group by 编码 having count(1) =2)


网站题目:sqlserver去重复,sqlserver去重复 保留一条
文章路径:http://ybzwz.com/article/dseicoo.html