MSSQL2000取最后日期物料数据(有重复的前提下)
时间:2011-11-11
来源:互联网
billdate , goodsid, userdef1, userdef2, userdef3, userdef4
2011-11-01 , 1001 , a ,b , c, d
2011-11-01 , 1001 , b , c , d ,e
2011-11-01 , 3002 , c ,d ,e ,f
2011-11-08 , 3002 , e ,f ,g ,h
2011-11-11 , 3013 , e ,f ,g ,h
要求结果如下: 相同GOODSID的行只取一行,不要重复
billdate , goodsid, userdef1, userdef2, userdef3, userdef4
2011-11-01 , 1001 , b , c , d ,e
2011-11-08 , 3002 , e ,f ,g ,h
2011-11-11 , 3013 , e ,f ,g ,h
作者: sankyqiu 发布时间: 2011-11-11
select * from tb t where billdate=(select max(billdate) from tb where goodsid=t.goodsid)
作者: fredrickhu 发布时间: 2011-11-11
if object_id('[TB]') is not null drop table [TB] go create table [TB] (billdate datetime,goodsid int,userdef1 nvarchar(2),userdef2 nvarchar(2),userdef3 nvarchar(2),userdef4 nvarchar(2)) insert into [TB] select '2011-11-01',1001,'a','b','c','d' union all select '2011-11-01',1001,'b','c','d','e' union all select '2011-11-01',3002,'c','d','e','f' union all select '2011-11-08',3002,'e','f','g','h' union all select '2011-11-11',3013,'e','f','g','h' select * from [TB] SELECT * FROM TB WHERE EXISTS(SELECT 1 FROM TB A WHERE TB.goodsid = goodsid AND TB.USERDEF1 >USERDEF1 ) /* billdate goodsid userdef1 userdef2 userdef3 userdef4 2011-11-01 00:00:00.000 1001 b c d e 2011-11-08 00:00:00.000 3002 e f g h*/
作者: OrchidCat 发布时间: 2011-11-11
--小F这个好,有单一值的时候就好用 select * from tb t where userdef1=(select max(userdef1) from tb where goodsid=t.goodsid) ORDER BY t.billdate ASC /* billdate goodsid userdef1 userdef2 userdef3 userdef4 2011-11-01 00:00:00.000 1001 b c d e 2011-11-08 00:00:00.000 3002 e f g h 2011-11-11 00:00:00.000 3013 e f g h*/
作者: OrchidCat 发布时间: 2011-11-11
作者: sankyqiu 发布时间: 2011-11-11
SQL code
--小F这个好,有单一值的时候就好用
select * from tb t where userdef1=(select max(userdef1) from tb where goodsid=t.goodsid)
ORDER BY t.billdate ASC
谢谢你们的回复,但如果USERDEF1也相同时如何取数呢???
作者: sankyqiu 发布时间: 2011-11-11
作者: sufull88 发布时间: 2011-11-12
关注一下,billdate , goodsid, userdef1均相同的重复行,如何处理呢?
要有效率就增加一个主健
作者: roy_88 发布时间: 2011-11-12
select * from tb as a where not exists(select 1 from tb where goodsid=a.goodsid and (billdate>a.billdate or (billdate=a.billdate and userdef1>a.userdef1) or (billdate=a.billdate and userdef1=a.userdef1 and userdef2>a.userdef2) or (billdate=a.billdate and userdef1=a.userdef1 and userdef2=a.userdef2 and userdef3>a.userdef3) or (billdate=a.billdate and userdef1=a.userdef1 and userdef2=a.userdef2 and userdef3=a.userdef3 and userdef4>a.userdef4) ) )
如果全部列都有重复,在select 再加上distinct
作者: roy_88 发布时间: 2011-11-12
作者: abclm 发布时间: 2011-11-12
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28