一、需求
假设数据表PrinterStatusLog:
No CmnGUID LifeCount Date
1 'FFF3D9' 10 2013-03-18 20:00:13
2 'FFF3D9' 10 2013-03-18 20:00:14
3 'FFF3D9' -1 2013-03-18 20:00:15
4 'FFF3D0' 10 2013-03-18 20:00:13
5 'FFF3D0' 10 2013-03-18 20:00:14
给定一组CmnGUID值(如:'FFF3D9','FFF3D0')在数据库中找出该表中每个CmnGUID对应的最新一条数据且LifeCount不能为-1。
如给出的CmnGUID为:'FFF3D9','FFF3D0'应该得到2、5两条数据。
二、问题
我现在用下面SQL查询时1、2、4、5都出来了,而1、4两条数据是不需要的。
SELECT "PSL"."CmnGUID","PSL"."Date","PSL"."LifeCount" FROM "PrinterStatusLog" "PSL"
WHERE "CmnGUID" in ('FFF3D9','FFF3D0')
AND "PSL"."LifeCount" <> -1 ORDER BY "PSL"."Date" DESC
求高手~
分不够了、不要介意啊~
SQL 数据库
------解决方案--------------------
SELECT * FROM Table WHERE CmnGUID in ('FFF3D9','FFF3D0') AND LifeCount <> -1 AND Date IN(SELECT MAX(Date) FROM Table where CmnGUID in ('FFF3D9','FFF3D0') GROUP BY CmnGUID)