求这句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
联系日
相关问答:
错误信息:
Microsoft OLE DB Provider for ODBC Drivers (0x80004005)
[Microsoft][ODBC SQL Server Driver][Shared Memory]无效的连接。
/bbsxp/test.asp, 第 24 行
机器配置:xp sp3 ,SQL 2005(vs ......
@s='S1,S2,S12,S23';
将@s里面的S1,S2替换成处理,未处理
来者有分.
REPLACE(@s,'S1,S2','处理,未处理')
REPLACE 不好用 自己试下
SQL code:
declare @s ......
姓名 学科
a 语文
a 数学
b 英语
b 政治
我想得到 a 语文,数学
......