在线咨询
QQ咨询
服务热线
服务热线:13125520620
TOP

SQL应用--查询最大版本值-数据库

发布时间:2011-11-12 浏览:4766

EmsNo        CopGNo         ExgVersion      

E57705000004 DSHC0741           60                                 
E57705000003 DSHC0741           50 
E57705000002 DSHC0741           36
E57705000001 DSHC0742           65                                        
E57705000000 DSHC0742           75  
需要的结果:
EmsNo        CopGNo         ExgVersion      
 
E57705000004 DSHC0741           60                                  
E57705000000 DSHC0742           75  
既 CopGNO中每个产品的最大ExgVersion给取出来。

SELECT T.EMSNO,E.COPGNO,E.EXGVERSION ,T.begindate,t.enddate
FROM emsexgversion T ,(SELECT copgno,MAX(ExgVersion) as exgversion from emsexgversion group by copgno) E
WHERE T.COPGNO=E.COPGNO AND T.EXGVERSION=E.EXGVERSION
ORDER BY E.COPGNO

select a.* from emsexgversion a
where not exists(select 1 from emsexgversion where CopGNo=a.CopGNo and ExgVersion>a.ExgVersion)

select 1 from ...
通常存在于exists中.
也可以写做
select 2 from ...
或者任何常数.

因为exists只是判断为不为null来返回true或者false
通常的写法还有
select count(1) from ...
其实等同于
select count(*) from ...
或者
select count(主键) from ...
这样的SQL

根据主键及索引的建立方式,速度上有细小的差别.

而-1 = -1这样的表达式是永远返回true的.
就像 1 = 2是永远返回false一样.

我们通常copy表结构就经常用
create table newtable as
select * from oldtable where 1 = 2;

-1 = -1一般是写在条件不确定的用字符串拼出来的动态SQL中.
比方说:
我的where后面可能有5个条件,也可能一个也没有.
这时我会这样写
".... where 1 = 1"+str1+str2+...; -- 其中的str1,str2等有可能是空字符串.

TAG
软件定制,软件开发,瀚森HANSEN
0
该内容对我有帮助