易截截图软件、单文件、免安装、纯绿色、仅160KB

简单的SQL语法,在线等 - MS-SQL Server / 基础类

我有一个用来做日志的数据表,表结构如下

源 时间 信息  
a 12:30 OOXX  
a 12:40 XXOO  
b 9:00 OXOX  
b 13:17 XOXO
a 15:40 OXXO

现在我想用最简单的语法来取得所有源的最近一条信息,SQL应该怎样写呢?我只会用TOP 1 order by 这个方法
我想取得的结果是
a 15:40 OXXO
b 13:17 XOXO

在线等
SQL code:
select * from tb t where 时间=(select max(时间) from tb where 源=t.源)


引用

SQL code
select * from tb t where 时间=(select max(时间) from tb where 源=t.源)

你的这个写法有问题的

SQL code:
select * from tb t where cast(时间 as datetime)=(select max(cast(时间 as datetime)) from tb where 源=t.源)


SQL code:
SELECT * from [table] a WHERE NOT exists(SELECT 1 from [Table] WHERE 源=a.源 and RIGHT('0'+时间,5)>RIGHT('0'+a.时间,5))


SQL code:

select * from tb t where not exists(select 1 from tb s where t.源=s.


相关问答:

jsp链接sql2000的疑问?



type Exception report


message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jas ......

Asp+sql server问题 - Web 开发 / ASP

我一个项目,有个插入操作,具体是这样的:
我有进货信息表。在出货时选择相应的进货信息,输入数量,选择部门后,点保存按钮,由于网络延时,点一下没有反映,于是用户就又点一下,导致一次插入了两条记录:
例: ......

请教SQL语句查询问题? - MS-SQL Server / 基础类

我想查询出每天数据的最大的一个值。表的格式如下
表名: hisdata
字段 编号 值 状态 时间  
  Id value state dattime  
  101 32.3 0 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号