sql2005
表A
id readTime
0025E220 2014-11-01 10:00:02
0025E220 2014-11-02 15:00:02
0125E220 2014-11-29 10:08:01
1025E220 2014-11-29 11:00:02
1125E220 2014-11-29 11:20:02
id字段中,后6位是一样的,只是前面2位不一样,这是同一个设备,前面2位代表设备状态
表B
status_id status_name
00 正常
01 异常
10 拆开
11 报警
我现在要获取表A中同一个设备最新读取时间,判断同一个设备用设备号的后6位数据,同时根据这个设备最新读取时间对应的8位设备号中的前2位,获取表B中对应的status_name. 这里要用到分组 group by
select A.id , substring(A.id, 2, 6) as id2, max(A.readTime), B.status_name from A, B where left(A.id, 2) = B.status_id group by A.id, substring(A.id, 2, 6) ,B.status_name
这样子写不对吗?
------解决思路----------------------
这样写没问题啊