SQL查询如何去重复数据?

qq_38192460 2017-04-05 04:07:21


我这里有两个表,表的属性如上



表2的内容



查询的时候,因为表2的每件商品的addTime属性有不同的时间,我能不能筛选出一个最新的时间,查询时只留下每一件商品的最新录入时间
像下面这样:



请各位论坛大牛来帮忙解决小萌新的问题,谢谢
...全文
874 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
吉普赛的歌 2017-04-05
  • 打赏
  • 举报
回复
USE tempdb
GO
IF OBJECT_ID('table1') IS NOT NULL
DROP TABLE table1
IF OBJECT_ID('table2') IS NOT NULL
DROP TABLE table2

create table table1 (lid int,name nvarchar(500));
insert into table1 (lid,name)
values (1,'本子'),(2,'牙膏');
 
create table table2 (lid int,addTime datetime);
insert into table2 (lid,addTime)
values  (1,'2015-09-14 23:59:59'),
        (1,'2015-09-15 23:59:59'),
        (2,'2015-09-15 23:59:59'),
        (2,'2015-09-14 23:59:59');

SELECT t.name,t.addTime FROM (     
	SELECT ROW_NUMBER() OVER (PARTITION BY a.lid ORDER BY b.addTime DESC) AS rid
	,a.NAME
	,b.addtime  
	FROM table1 AS a INNER JOIN table2 AS b ON a.lid=b.lid
) AS t
WHERE rid=1
/*
name	addTime
本子	2015-09-15 23:59:59.000
牙膏	2015-09-15 23:59:59.000
*/
二月十六 2017-04-05
  • 打赏
  • 举报
回复
引用 15 楼 qq_38192460 的回复:
[quote=引用 11 楼 sinat_28984567 的回复:] [quote=引用 8 楼 qq_38192460 的回复:] [quote=引用 7 楼 sinat_28984567 的回复:] 贴点测试数据吧
select name,price,barCode,proFormat,addTime from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID [/quote] 好吧,其实我是想这样,你看看大版主写的吧。http://bbs.csdn.net/topics/391996442 然后我自己随便写了点测试数据,感觉应该没问题。也可能是哪里没看好,你参考一下。
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T(gid INT,[name] nvarchar(22),[proFormat] text,[addTime] DATETIME)
Insert #T
select 1,N'测试','12','2017-01-01 00:00:00' UNION 
select 1,N'测试','12','2017-02-01 00:00:00' UNION 
select 1,N'测试','12','2017-03-01 00:00:00' UNION 
select 2,N'测试','12','2017-04-01 00:00:00' UNION 
select 2,N'测试','12','2017-05-01 00:00:00' 
GO
if not object_id(N'Tempdb..#T1') is null
	drop table #T1
Go
Create table #T1([id] int,[商品名] nvarchar(23))
Insert #T1
select 1,N'商品1' union all
select 2,N'商品2'
Go
--测试数据结束
SELECT  name ,
        CONVERT(VARCHAR(8000), proFormat) AS proFormat ,
        b.商品名 ,
        MAX(addTime)
FROM    #T a
        JOIN #T1 b ON a.gid = b.id
GROUP BY CONVERT(VARCHAR(8000), proFormat) ,
        name ,
        商品名
[/quote] 好了,解决了,原来之前是group by 多加了 addTime,谢谢大神的耐心解答![/quote] 记得结贴哦
qq_38192460 2017-04-05
  • 打赏
  • 举报
回复
引用 11 楼 sinat_28984567 的回复:
[quote=引用 8 楼 qq_38192460 的回复:] [quote=引用 7 楼 sinat_28984567 的回复:] 贴点测试数据吧
select name,price,barCode,proFormat,addTime from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID [/quote] 好吧,其实我是想这样,你看看大版主写的吧。http://bbs.csdn.net/topics/391996442 然后我自己随便写了点测试数据,感觉应该没问题。也可能是哪里没看好,你参考一下。
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T(gid INT,[name] nvarchar(22),[proFormat] text,[addTime] DATETIME)
Insert #T
select 1,N'测试','12','2017-01-01 00:00:00' UNION 
select 1,N'测试','12','2017-02-01 00:00:00' UNION 
select 1,N'测试','12','2017-03-01 00:00:00' UNION 
select 2,N'测试','12','2017-04-01 00:00:00' UNION 
select 2,N'测试','12','2017-05-01 00:00:00' 
GO
if not object_id(N'Tempdb..#T1') is null
	drop table #T1
Go
Create table #T1([id] int,[商品名] nvarchar(23))
Insert #T1
select 1,N'商品1' union all
select 2,N'商品2'
Go
--测试数据结束
SELECT  name ,
        CONVERT(VARCHAR(8000), proFormat) AS proFormat ,
        b.商品名 ,
        MAX(addTime)
FROM    #T a
        JOIN #T1 b ON a.gid = b.id
GROUP BY CONVERT(VARCHAR(8000), proFormat) ,
        name ,
        商品名
[/quote] 好了,解决了,原来之前是group by 多加了 addTime,谢谢大神的耐心解答!
二月十六 2017-04-05
  • 打赏
  • 举报
回复
SELECT  name ,
MAX(addTime) AS addTime
FROM dbo.table1 a
JOIN dbo.table2 b ON a.lid = b.proID
GROUP BY name


qq_38192460 2017-04-05
  • 打赏
  • 举报
回复
引用 12 楼 sinat_28984567 的回复:
[quote=引用 10 楼 yenange 的回复:] [quote=引用 8 楼 qq_38192460 的回复:] [quote=引用 7 楼 sinat_28984567 的回复:] 贴点测试数据吧
select name,price,barCode,proFormat,addTime from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID [/quote] 人家希望你贴下面这些东西: create table ProInfor( xxx int,xxx ) create Storehouse( xxx int,xxx ) insert into ProInfor ... insert into Storehouse... 没有示例的脚本, 人家帮你的成本很高, 而且也很难说清楚 [/quote] [/quote] 非常抱歉,因为初到论坛,规矩啥的还不懂 这是测试数据:

create table table1 (lid int,name nvarchar(500));
insert into table1 (lid,name)
values (1,'本子'),(2,'牙膏');

create table table2 (lid int,addTime datetime);
insert into table2 (proID,addTime)
values  (1,'2015-09-14 23:59:59'),
		(1,'2015-09-15 23:59:59'),
		(2,'2015-09-15 23:59:59'),
		(2,'2015-09-14 23:59:59');
表名:table1 lid name --列名 1 本子 2 牙膏 表名:table2 lid addTime --列名 1 2015-09-14 23:59:59 1 2015-09-15 23:59:59 2 2015-09-14 23:59:59 2 2015-09-15 23:59:59 --要实现的效果: name addTime ------- ----------- ----------- ----------- ----------- ----------- 本子 2015-09-15 23:59:59 牙膏 2015-09-15 23:59:59
二月十六 2017-04-05
  • 打赏
  • 举报
回复
引用 10 楼 yenange 的回复:
[quote=引用 8 楼 qq_38192460 的回复:] [quote=引用 7 楼 sinat_28984567 的回复:] 贴点测试数据吧
select name,price,barCode,proFormat,addTime from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID [/quote] 人家希望你贴下面这些东西: create table ProInfor( xxx int,xxx ) create Storehouse( xxx int,xxx ) insert into ProInfor ... insert into Storehouse... 没有示例的脚本, 人家帮你的成本很高, 而且也很难说清楚 [/quote]
二月十六 2017-04-05
  • 打赏
  • 举报
回复
引用 8 楼 qq_38192460 的回复:
[quote=引用 7 楼 sinat_28984567 的回复:]
贴点测试数据吧


select name,price,barCode,proFormat,addTime
from ProInfor,Storehouse
where ProInfor.lid=Storehouse.proID
[/quote]


好吧,其实我是想这样,你看看大版主写的吧。http://bbs.csdn.net/topics/391996442

然后我自己随便写了点测试数据,感觉应该没问题。也可能是哪里没看好,你参考一下。
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T(gid INT,[name] nvarchar(22),[proFormat] text,[addTime] DATETIME)
Insert #T
select 1,N'测试','12','2017-01-01 00:00:00' UNION
select 1,N'测试','12','2017-02-01 00:00:00' UNION
select 1,N'测试','12','2017-03-01 00:00:00' UNION
select 2,N'测试','12','2017-04-01 00:00:00' UNION
select 2,N'测试','12','2017-05-01 00:00:00'
GO
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([id] int,[商品名] nvarchar(23))
Insert #T1
select 1,N'商品1' union all
select 2,N'商品2'
Go
--测试数据结束
SELECT name ,
CONVERT(VARCHAR(8000), proFormat) AS proFormat ,
b.商品名 ,
MAX(addTime)
FROM #T a
JOIN #T1 b ON a.gid = b.id
GROUP BY CONVERT(VARCHAR(8000), proFormat) ,
name ,
商品名



吉普赛的歌 2017-04-05
  • 打赏
  • 举报
回复
引用 8 楼 qq_38192460 的回复:
[quote=引用 7 楼 sinat_28984567 的回复:] 贴点测试数据吧
select name,price,barCode,proFormat,addTime from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID [/quote] 人家希望你贴下面这些东西: create table ProInfor( xxx int,xxx ) create Storehouse( xxx int,xxx ) insert into ProInfor ... insert into Storehouse... 没有示例的脚本, 人家帮你的成本很高, 而且也很难说清楚
qq_38192460 2017-04-05
  • 打赏
  • 举报
回复
引用 4 楼 mahuatengBC 的回复:
select distinct name,price,barCode,proFormat, from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID order by addTime desc 试试这个
用distinct没有用,因为一个产品有多个addTime,而我只希望筛选出最新的时间一条
qq_38192460 2017-04-05
  • 打赏
  • 举报
回复
引用 7 楼 sinat_28984567 的回复:
贴点测试数据吧
select name,price,barCode,proFormat,addTime from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID
二月十六 2017-04-05
  • 打赏
  • 举报
回复
贴点测试数据吧
qq_38192460 2017-04-05
  • 打赏
  • 举报
回复
引用 5 楼 sinat_28984567 的回复:
把proFormat改成CONVERT(VARCHAR(8000)
SELECT  name ,
CONVERT(VARCHAR(8000), proFormat) AS proFormat ,其他字段
MAX(addTime)
FROM #T
GROUP BY name ,
CONVERT(VARCHAR(8000), proFormat),其他字段


运行结果如下,我发现只是去掉了重复的addTime属性
二月十六 2017-04-05
  • 打赏
  • 举报
回复
把proFormat改成CONVERT(VARCHAR(8000)
SELECT  name ,
        CONVERT(VARCHAR(8000), proFormat) AS proFormat ,其他字段
        MAX(addTime)
FROM    #T
GROUP BY name ,
        CONVERT(VARCHAR(8000), proFormat),其他字段
mahuatengBC 2017-04-05
  • 打赏
  • 举报
回复
select distinct name,price,barCode,proFormat, from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID order by addTime desc 试试这个
qq_38192460 2017-04-05
  • 打赏
  • 举报
回复
引用 1 楼 sinat_28984567 的回复:
select name,price,等等其他字段,max(时间) from 表1 ,表2 where 表1.lid = 表2.proid group by name,price,等等其他字段
很感谢你的回复,我试了试 select name,price,barCode,proFormat,max(addTime) from ProInfor,Storehouse where ProInfor.lid=Storehouse.proID group by name,price,barCode,proFormat,addTime 结果不能成功执行,返回 “不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。” 忘了说proFormat列是text类型的,我特意在图里面标出来的
二月十六 2017-04-05
  • 打赏
  • 举报
回复
最好不要贴图。。给点测试数据和想要的结果,方便其他人写语句
二月十六 2017-04-05
  • 打赏
  • 举报
回复
select name,price,等等其他字段,max(时间) from 表1 ,表2 where 表1.lid = 表2.proid group by name,price,等等其他字段

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧