在sql 中把table一txt列内datatable 过滤重复行的内容删去?

过滤同一个表中多个字段重复数据的SQL技巧
对于数据:
ID Name Code
---------------------------
希望通过一个SQL语句过滤出形如:
可使用至少两种方式来做:
SELECT CODE,NAME,COUNT(*) AS [COUNT],MAX(ID) AS ID
GROUP BY CODE,NAME HAVING COUNT(*) & 1
SELECT * FROM (SELECT NAME,CODE,(SELECT COUNT(*) FROM TEST
WHERE NAME=A.NAME AND CODE=A.CODE ) AS [COUNT],MAX(ID) AS ID
FROM TEST A
GROUP BY NAME,CODE) RES
WHERE [COUNT] &
SELECT&&&&
FROM&&&&&&&&
PE_CommonModel AS a
(Title NOT IN
&&&&&&&&&&&&&&&&&&&&&&&&&
(SELECT&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
FROM&&&&&&&&&
PE_CommonModel AS b
&&&&&&&&&&&&&&&&&&&&&&&&&&&
GROUP BY Title
&&&&&&&&&&&&&&&&&&&&&&&&&&&
HAVING&&&&&
(COUNT(*) & 1))) AND (Status = 99)
ORDER BY ItemID DESC
一。查找重复记录
1。查找全部重复记录
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段
Having Count(*)&1)
2。过滤重复记录(只显示一条)
Select * From HZT Where ID In (Select Max(ID) From HZT Group By
注:此处显示ID最大一条记录
二。删除重复记录
1。删除全部重复记录(慎用)
Delete 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having
Count(*)&1)
2。保留一条(这个应该是大多数人所需要的 ^_^)
Delete HZT Where ID Not In (Select Max(ID) From HZT Group By
注:此处保留ID最大一条记录
有一个表的结构是这样的
id bookname cbs zz
001 asp大全 清华大学出版社 田正平
002 数据库系统教程 高等教育出版社 周奥英
003 asp大全 清华大学出版社 田正平
现在我想做一个查询,要查找出的字段如下
bookname, cbs, zz
按bookname字段过滤记录,但要显示多个字段
不知道上面的回答怎么样?不过下面的语句已经经过千锤百炼,呵呵。试试吧
select bookname,cbs,zz from table where id in(select min(id) from
table group by bookname)
select t1.bookname,t1.zz,t1.cbs from table t1
join ( select min(id) as id,bookname from table
group by bookname ) as t2
on t1.bookname = t2.bookname and t1.id = t2.id
有一个表的结构是这样的
id bookname cbs zz
001 asp大全 清华大学出版社 田正平
002 数据库系统教程 高等教育出版社 周奥英
003 asp大全 清华大学出版社 田正平
现在我想做一个查询,要查找出的字段如下
bookname, cbs, zz
按bookname字段过滤记录,但要显示多个字段
SELECT t1.CardID, t1.CardValue, t1.RecordID
FROM eConsumeData t1 INNER JOIN
(SELECT Max(recordid) AS recordid, cardid
FROM eConsumeData
GROUP BY cardid) t2 ON t1.CardID = t2.cardid AND
t1.RecordID = t2.recordid
ORDER BY t2.cardid
有重复时筛选出1条,没重复时显示,即把重复信息过滤掉
SELECT&&&&
GeneralID, NodeID, ModelID, ItemID, TableName, Title, Inputer,
Hits, DayHits, WeekHits, MonthHits, LinkType, UpdateTime,
CreateTime, TemplateFile,
&&&&&&&&&&&&&&&&&&&&&
Status, EliteLevel, Priority, CommentAudited, CommentUnAudited,
SigninType, InputTime, PassedTime, Editor, LastHitTime,
DefaultPicUrl, PinyinTitle,
&&&&&&&&&&&&&&&&&&&&&
TitleFontColor, TitleFontType, IncludePic, ShowCommentLink,
TitleHashKey, HtmlPageName, SGType, SGDataId
FROM&&&&&&&&
PE_CommonModel AS CM
((GeneralID IN (SELECT&MAX(GeneralID) AS Expr1
FROM&PE_CommonModel GROUP BY ItemID HAVING
(COUNT(*) & 1))) OR (ItemID NOT IN (SELECT ItemID
FROM PE_CommonModel AS b GROUP BY ItemID HAVING (COUNT(*)
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。SQL根据现有表新建一张表,想新建一张表,新建的这张表结构要跟现有表结构相同,但不要现有表里面的数据!_百度知道
SQL根据现有表新建一张表,想新建一张表,新建的这张表结构要跟现有表结构相同,但不要现有表里面的数据!
根据现有表新建一张表,想新建一张表,新建的这张表结构要跟现有表结构相同,但不要现有表里面的数据!说白了,就是想复制现有表的表结构而不要现有表里的数据!!这个SQL怎么写、、
提问者采纳
看你用的什么数据库:
Sql server :
select * into table_new from table_ 复制结构和数据
select * into table_new from table_old where 1=2;只复制结构 Oracle:
create table table_new as select * from table_复制结构和数据
create table table_new as select * from table_old where 1=0;只复制结构 DB2:
--复制表结构
create table table_name_new as (select * from table_name_old)
--插入数据
insert into table_name_new (select * from table_name_old); MySql:
----- 复制表结构及数据到新表
CREATE TABLE 新表
SELECT * FROM 旧表
----- 只复制表结构到新表
CREATE TABLE 新表
SELECT * FROM 旧表 WHERE 1=2
其他类似问题
为您推荐:
其他3条回答
create table newtable asselect * from oldtable where 1=2
SQLSERVER:select * into b from a where 1 && 1 或者select top 0 * into b from aa是原表,b是现有表。 ORACLE:create table b as select * from a where 1 && 1
//复制了表student 的表结构到 studets中了select top 0 * into studets from student
您可能关注的推广回答者:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁sql语句删除 两列重复(都是外键) 的数据,但是需要保留一条 表名 SMS_GroupContactInfo_百度知道
提问者采纳
没有环境 可能稍微有点问题
你试一下吧!~delete SMS_GroupContactInfo where GroupID + '---' + ContactID
= (select max(GroupID + '---' + ContactID) as aa from SMS_GroupContactInfo group by GroupID + '---' + ContactID having count(GroupID + '---' + ContactID) & 1
麻烦在 环境里 测哈,THANKS!
报错了吗? 如果报错把错误内容发出来!~
Msg 102, Level 15, State 1, Line 1Incorrect syntax near 'SMS_GroupContactInfo'.Msg 102, Level 15, State 1, Line 1Incorrect syntax near '1'.
delete from SMS_GroupContactInfo where GroupID + '---' + ContactID
= (select max(GroupID + '---' + ContactID) as aa from SMS_GroupContactInfo group by GroupID + '---' + ContactID having count(GroupID + '---' + ContactID) & 1忘写from了!~
还有 错误 ,劳驾。。。Msg 102, Level 15, State 1, Line 4Incorrect syntax near '1'.
delete from SMS_GroupContactInfo where GroupID + '---' + ContactID
= (select max(GroupID + '---' + ContactID) as aa from SMS_GroupContactInfo group by GroupID + '---' + ContactID having count(GroupID + '---' + ContactID) & 1)
Msg 245, Level 16, State 1, Line 1Conversion failed when converting the varchar value '---' to data type int.
怎么会有converting呢!~
我试过了没有问题的啊!~ 你的这两个id还有数值型的?
GroupID ,ContactID
都是外键,自动增长的
delete from SMS_GroupContactInfo where rtrim(GroupID) + '---' + rtrim(ContactID)
= (select max(rtrim(GroupID) + '---' + rtrim(ContactID)) as aa from SMS_GroupContactInfo group by rtrim(GroupID) + '---' + rtrim(ContactID)having count(rtrim(GroupID) + '---' + rtrim(ContactID)) & 1)转一下吧!~ 这样做保险点!~
还有 写 错误哦
,,,想 采纳你 的,但是 问题么有解决Msg 512, Level 16, State 1, Line 1Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, &, &= , &, &= or when the subquery is used as an expression.Warning: Null value is eliminated by an aggregate or other SET operation.
写错东西了!~ 这把应该没问题了!~delete from SMS_GroupContactInfo where ID
= (select max(ID) as aa from SMS_GroupContactInfo group by rtrim(GroupID) + '---' + rtrim(ContactID)having count(rtrim(GroupID) + '---' + rtrim(ContactID)) & 1)
你这样写 ,在重复数据中只会删除一条ID大的数据,而我的重复数据重复不止两条,有三条,或四条,我要保留最小ID ,麻烦 在 改改 。。。thanks
delete from SMS_GroupContactInfo where ID
in (select ID from SMS_GroupContactInfo where id not in (select min(ID) as aa from SMS_GroupContactInfo group by rtrim(GroupID) + '---' + rtrim(ContactID)having count(rtrim(GroupID) + '---' + rtrim(ContactID)) & 1)having count(rtrim(GroupID) + '---' + rtrim(ContactID)) & 1)
来自团队:
其他类似问题
为您推荐:
其他2条回答
table tables( id int,
na varchar(10))insert into tables values (1,'11')insert into tables values (2,'11')insert into tables values (3,'22')insert into tables values (4,'22')insert into tables values (5,'11')delete from tableswhere id
in (select id
from tables where na='11' ) and id not in (select min(id) from
tables where na='11') select * from tables如此题 思路
删除多条相同的数据
只保留id最小的那一条
单条命令是不好完成这个任务的, 思路应该是这样: ①找出有重复的数据,保存到临时表 ②删除原表中的记录 ③ 临时表的数据 通过 unique 之类的 过虑为不重复数据 ④插入回原表分解好的 应该容易完成吧
sql语句的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁紧急求救,SQL Server中的表被drop table误删除了,能否恢复
[问题点数:100分,结帖人peters]
紧急求救,SQL Server中的表被drop table误删除了,能否恢复
[问题点数:100分,结帖人peters]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关推荐:
2005年2月 总版技术专家分月排行榜第三
2006年4月 荣获微软MVP称号2008年10月 荣获微软MVP称号2007年4月 荣获微软MVP称号
2005年4月 .NET技术大版内专家分月排行榜第二
2005年6月 .NET技术大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 datatable去除重复行 的文章

 

随机推荐