这个SQL如何写

yousite1 2007-12-04 01:55:59
有合同明细表,结构如下:
GoodsNO 商品编号
GoodsName 商品名称
Brand 品牌
Marque 商品型号
AgreementCode 合同编号
BegDate 合同生效日期
EndDate 合同失效日期
SignDate 签约日期
Signer 签合同人
LastUpdate 最后更改时间

我想实现读出合同单号列表,每个合同对应多条商品信息,我想实现只不重复的读出合同号,并且按最后更改时间排序
发现这样写 SELECT DISTINCT AgreementCode FROM Mall_StoreGoods Order by LastUpdate desc会
出错,请帮我看一下要怎么写这个SQL才好
...全文
115 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2007-12-04
  • 打赏
  • 举报
回复
select a.* from tb a where LastUpdate = (select max(LastUpdate) from tb where AgreementCode = a.AgreementCode) order by a.LastUpdate desc
-狙击手- 2007-12-04
  • 打赏
  • 举报
回复
select *
from Mall_StoreGoods a
where not exists(select 1 from Mall_StoreGoods where AgreementCode = a.AgreementCode and LastUpdate > a.LastUpdate)
dawugui 2007-12-04
  • 打赏
  • 举报
回复
select a.* from tb a where 最后更改时间 = (select max(最后更改时间) from tb where 合同编号 = a.合同编号) order by a.最后更改时间 desc
kk19840210 2007-12-04
  • 打赏
  • 举报
回复
select AgreementCode ,LastUpdate=max(LastUpdate) from Mall_StoreGoods group by AgreementCode order by max(LastUpdate) desc
-狙击手- 2007-12-04
  • 打赏
  • 举报
回复
SELECT DISTINCT AgreementCode FROM Mall_StoreGoods Order by LastUpdate desc


------
语法上没有错呀,

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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