求这句Sql如何理解
create table [客户联系表]([客户编号] varchar(2),[联系日期] datetime,[联系内容] int)
insert [客户联系表]
select 'aa','2009/01/01',111 union all
select 'bb','2009/02/01',222 union all
select 'aa','2009/03/01',333 union all
select 'aa','2009/04/01',444 union all
select 'bb','2009/05/01',555
select b.客户编号, b.联系日期,b.联系内容 from 客户联系表 b where not exists(select 1 from 客户联系表 where 客户编号=b.客户编号 and 联系日期 >b.联系日期)
结果为:aa 2009-04-01 00:00:00.000 444
bb 2009-05-01 00:00:00.000 555
求:not exists这句如何理解
不明白为啥求出来就是最后日期
不包含!!
相同客户编号,显示最近联系的
大哥,拿这个例子讲详细点
not exists 再加上联系日期 >b.联系日期,就像双重否定的意思,最后得到b的结果就是最新的联系日期
where not exists(select 1 from 客户联系表 where 客户编号=b.客户编号 and 联系日期 >b.联系日期)
相当于
where 联系日期 not in(select top 1 联系日期 from 客户联系表 where 客户编号=b.客户编号 and 联系日期 >b.联系日期)
select b.客户编号, b.联系日期,b.联系内容 from 客户联系表 b where not exists(select 1 from 客户联系表 where 客户编号=b.客户编号 and 联系日期 >b.联系日期)
客户编号=b.客户编号 结果为aa和bb
联系日
相关问答:
自从直接在一个表里加了个字段(nvarchar 其他默认,不插入自己默认为 <null>)后,重启网站+sql server 2000 6-8个小时后出现问题:网站写数据库时脚本超时
Active Server Pages 错误 'ASP 0113 ......
如题:前几天服务器还是好好的 今天网页打不开突然出现错误,一查是链接不上数据库了?
(查询分析器和企业管理器都可以正常显示)
[SqlException (0x80131904): 用户 'sa' 登录失败。]
......
现在我的情况是这样:
我的 ......
@s='S1,S2,S12,S23';
将@s里面的S1,S2替换成处理,未处理
来者有分.
REPLACE(@s,'S1,S2','处理,未处理')
REPLACE 不好用 自己试下
SQL code:
declare @s ......
客户表:
客户编号 客户销售总额
aa 200
bb 300
cc ......